import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @author cenguocheng [email protected]
* @date 2019/10/11 13:55
*/
public class TestSort implements Comparator<Persion> {
@Override
public int compare(Persion o1, Persion o2) {
int seq1 = 0;
int seq2 = 0;
try {
seq1 = o1.getSort();
seq2 = o2.getSort();
} catch (Exception e) {
e.printStackTrace();
}
return seq1 - seq2;
}
public static void main(String[] args) {
List<Persion> testSorts = new ArrayList<Persion>();
for (int i = 0; i < 10; i++) {
int sort = (int)(Math.random()*100);
testSorts.add(new Persion("JACK:"+sort,sort));
System.out.println("JACK:"+sort+" > "+sort);
}
System.out.println("---------------排序後---------------");
Collections.sort(testSorts, new TestSort());
for (Persion testSort:testSorts) {
System.out.println("sort: "+testSort.getName()+" > "+testSort.getSort());
}
}
}
class Persion{
public Persion() { }
public Persion(String name, int sort) {
this.name = name;
this.sort = sort;
}
public String name;
public int sort;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSort() {
return sort;
}
public void setSort(int sort) {
this.sort = sort;
}
}