package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class TestA
{
int a;
int b;
public TestA(int a, int b)
{
this.a = a;
this.b = b;
}
public int getA()
{
return a;
}
public int getB()
{
return b;
}
}
public class TestSort
{
/**
* @param args
*/
public static void main(String[] args)
{
ArrayList<TestA> myList = new ArrayList<TestA>();
myList.add(new TestA(2, 4));
myList.add(new TestA(2, 3));
myList.add(new TestA(7, 23));
myList.add(new TestA(22, 1));
myList.add(new TestA(6, 3));
Collections.sort(myList, new Comparator<TestA>()
{
@Override
public int compare(TestA o1, TestA o2)
{
if(o1.getA() > o2.getA())
return 1;
else if(o1.getA() < o2.getA())
return -1;
else return 0;
}
});
for (TestA e : myList)
{
System.out.println(e.getA() + " " + e.getB());
}
}
}
自定义sort排序方法
最新推荐文章于 2024-01-07 18:59:31 发布