public class TestObjectSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person1[] ps = new Person1[]{new Person1("p0",0),
new Person1("p1",3),
new Person1("p2",5),
new Person1("p3",4),
new Person1("p4",8),
new Person1("p5",6),
new Person1("p6",7),
new Person1("p7",1),
new Person1("p8",2),
new Person1("p9",9)};
List<Person1> pl = new ArrayList<Person1>();
for(int i = 0; i < 10; i++){
System.out.print(ps[i].getAge());
pl.add(ps[i]);
}
System.out.println("/n使用Collections.sort(List, Comparator)类来比较:");
long l1 = System.currentTimeMillis();
Collections.sort(pl, new MyComparator());
System.out.println("time: " + (System.currentTimeMillis() - l1));
for(Iterator it = pl.iterator(); it.hasNext();){
Person1 p = (Person1) it.next();
System.out.print(p.getAge());
}
Person2[] ps2 = new Person2[]{new Person2("p0",0),
new Person2("p1",3),
new Person2("p2",5),
new Person2("p3",4),
new Person2("p4",8),
new Person2("p5",6),
new Person2("p6",7),
new Person2("p7",1),
new Person2("p8",2),
new Person2("p9",9)};
System.out.println("/n使用Arrays.sort(Object[])类来比较:");
long l2 = System.currentTimeMillis();
Arrays.sort(ps2);
System.out.println("time: " + (System.currentTimeMillis() - l2));
for(int i = 0; i < 10; i++){
System.out.print(ps2[i].getAge());
}
Person2[] ps3 = new Person2[]{new Person2("p0",0),
new Person2("p1",3),
new Person2("p2",5),
new Person2("p3",4),
new Person2("p4",8),
new Person2("p5",6),
new Person2("p6",7),
new Person2("p7",1),
new Person2("p8",2),
new Person2("p9",9)};
List<Person2> pl3 = new ArrayList<Person2>();
for(int i = 0; i < 10; i++){
pl3.add(ps3[i]);
}
System.out.println("/n使用Collections.sort(List)类来比较:");
Collections.sort(pl3);
for(Iterator it = pl3.iterator(); it.hasNext();){
Person2 p = (Person2) it.next();
System.out.print(p.getAge());
}
}
}
class MyComparator implements Comparator{
public int compare(Object o1, Object o2){
Person1 p1 = (Person1)o1;
Person1 p2 = (Person1)o2;
if(p1.getAge() < p2.getAge()){
return -1;
}else if(p1.getAge() == p2.getAge()){
return 0;
}else{
return 1;
}
}
}
class Person1{
private String name;
private int age;
public Person1(){}
public Person1(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Person2 implements Comparable{
private String name;
private int age;
public Person2(){}
public Person2(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int compareTo(Object o){
Person2 p = (Person2)o;
if(this.age < p.age){
return -1;
}else if(this.age == p.age){
return 0;
}else{
return 1;
}
}
}
-----------------------------------------------------Arrays.sort和Collections.sort补充资料-------------------------------------------------------------
1.我们使用Arrays对数组进行排序,使用Collections对结合框架容器进行排序,如ArraysList,LinkedList等。
Arrays.sort 对基本数据类型(primitive type)或String类型的数组进行排序:
对基本数据类型(primitive type)或String类型的数组进行排序
int[] intArray = new int[] {4, 1, 3, -23};
Arrays.sort(intArray);
// [-23, 1, 3, 4]
String[] strArray = new String[] {"z", "a", "C"};
Arrays.sort(strArray);
// [C, a, z]
// Case-insensitive sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
// [a, C, z]
// Reverse-order sort
Arrays.sort(strArray, Collections.reverseOrder());
// [z, a, C]
// Case-insensitive reverse-order sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(Arrays.asList(strArray));
// [z, C, a]
2.当然我们也可以指定数组的某一段进行排序比如我们要对数组下表0-2的部分(假设数组长度大于3)进行排序,其他部分保持不变,我们可以使用:
Arrays.sort(strArray,0,2);
这样,我们只对前三个元素进行了排序,而不会影响到后面的部分。
3.对对象数组进行排序,对象必须实现Comparable接口
这个数组的自然顺序是未知的,因此我们需要为该类实现Comparable接口
Name类
public class Name implements Comparable<Name>{
public String firstName, lastName;
public Name(String firstName,String lastName){
this.firstName=firstName;
this.lastName=lastName;
}
public int compareTo(Name o) { //实现接口
int lastCmp=lastName.compareTo(o.lastName);
return (lastCmp!=0?lastCmp:firstName.compareTo(o.firstName));
}
public String toString(){ //便于输出测试
return firstName+" "+lastName;
}
}
这样,当我们对这个对象数组进行排序时,就会先比较lastName,然后比较firstName 然后得出两个对象的先后顺序,就像compareTo(Name o)里实现的那样
用程序进行测试
NameSort类
import java.util.*;
public class NameSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
Name[] nameArray = new Name[]{
new Name("John", "Lennon"),
new Name("Karl", "Marx"),
new Name("Groucho", "Marx"),
new Name("Oscar", "Grouch")
};
Arrays.sort(nameArray);
for(int i=0;i<nameArray.length;i++){
System.out.println(nameArray[i].toString());
}
}
}
对集合框架进行排序
如果已经理解了Arrays.sort()对数组进行排序的话,集合框架的使用也是大同小异。只是将Arrays替换成了Collections,注意Collections是一个类而Collection是一个接口.
假如有这样一个链表:
LinkedList list=new LinkedList();
list.add(4);
list.add(34);
list.add(22);
list.add(2);
我们只需要使用:
Collections.sort(list);
就可以将ll里的元素按从小到大的顺序进行排序,结果就成了:
[2, 4, 22, 34]
如果LinkedList里面的元素是String,同样会想基本数据类型一样从小到大排序。
如果要实现反序排序也就是从大到小排序:
Collections.sort(list,Collections.reverseOrder()); 此方法实际是: Collections.sort(List<T>, Comparator<? super T>)