JAVA排序算法讨论-对象字段排序代码

14年前
package com.pip.compare;    import java.util.ArrayList;  import java.util.Collections;  import java.util.Comparator;  import java.util.List;    public class ListCompareUtil {     public static void sortList(List list) {    Comparator instance = new HashMapComparator();    Collections.sort(list, instance);   }     public static List<Bean> getTestList() {    List list = new ArrayList();    for (int i = 0; i < 10; i++) {     Bean bean = new Bean();     int index = i;     if (i == 2 || i == 5 || i == 6) {      index = 3;     } else {      index = index + 1;     }     bean.setName("fwang" + index);     list.add(bean);    }    return list;   }     public static void printList(List list) {    if (list != null && list.size() > 0) {     int count = 1;     for (Bean bean : list) {      count++;      String name = bean.getName();      System.out.println("name" + count + ":" + name);     }    }   }     public static void main(String args[]) {    List list = ListCompareUtil.getTestList();    ListCompareUtil.sortList(list);    ListCompareUtil.printList(list);   }  }    package com.pip.compare;    public class Bean {   private String id;   private String code;   private String name;   private String type;   private String status;     public String getId() {    return id;   }     public void setId(String id) {    this.id = id;   }     public String getCode() {    return code;   }     public void setCode(String code) {    this.code = code;   }     public String getName() {    return name;   }     public void setName(String name) {    this.name = name;   }     public String getType() {    return type;   }     public void setType(String type) {    this.type = type;   }     public String getStatus() {    return status;   }     public void setStatus(String status) {    this.status = status;   }  }    package com.pip.compare;    import java.util.Comparator;    @SuppressWarnings("rawtypes")  public class HashMapComparator implements Comparator {   public int compare(Object o1, Object o2) {    Bean bean1 = (Bean) o1;    Bean bean2 = (Bean) o2;      String name1 = bean1.getName();    String name2 = bean2.getName();      if (name1.hashCode() < name2.hashCode()) {     System.out.println("------------------->name1:" + name1 + " "       + name1.hashCode() + "\t name2:" + name2 + " "       + name2.hashCode());     return -1;    } else {     return 0;    }   }  }  
result:
------------------->name1:fwang1 -1261896216  name2:fwang2 -1261896215
------------------->name1:fwang2 -1261896215  name2:fwang3 -1261896214
------------------->name1:fwang3 -1261896214  name2:fwang4 -1261896213
------------------->name1:fwang4 -1261896213  name2:fwang5 -1261896212
------------------->name1:fwang3 -1261896214  name2:fwang8 -1261896209
------------------->name1:fwang8 -1261896209  name2:fwang9 -1261896208
------------------->name1:fwang9 -1261896208  name2:fwang10 -464076984
name2:fwang1
name3:fwang2
name4:fwang3
name5:fwang4
name6:fwang5
name7:fwang3
name8:fwang3
name9:fwang8
name10:fwang9
name11:fwang10