有幾個方法能夠實現:讓 Student 實現Comparable接口,或是實例化一個比較器,如今用 Comparator 比較器實例來作一個:java
ComparableTest.Java import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class ComparableTest { public static void main(String[] args) { Comparator<Student> comparator = new Comparator<Student>(){ public int compare(Student s1, Student s2) { //先排年齡 if(s1.age!=s2.age){ return s2.age-s1.age; } else{ //年齡相同則按姓名排序 if(!s1.name.equals(s2.name)){ return s2.name.compareTo(s1.name); } else{ //姓名也相同則按學號排序 return s2.id-s1.id; } } } }; Student stu1 = new Student (1,"zhangsan","male",28,"cs"); Student stu2 = new Student (2,"lisi","female",19,"cs"); Student stu3 = new Student (3,"wangwu","male",22,"cs"); Student stu4 = new Student (4,"zhaoliu","female",17,"cs"); Student stu5 = new Student (5,"jiaoming","male",22,"cs"); ArrayList<Student> List = new ArrayList<Student>(); List.add(stu1); List.add(stu2); List.add(stu3); List.add(stu4); List.add(stu5); //這裏就會自動根據規則進行排序 Collections.sort(List,comparator); display(List); } static void display(ArrayList<Student> lst){ for(Student s:lst) System.out.println(s); } } class Student{ int age; int id; String gender; String name; String cs; Student(int id,String name,String gender,int age,String cs){ this.age=age; this.name=name; this.gender=gender; this.id=id; this.cs=cs; } public String toString(){ return id+" "+name+" "+gender+" "+age+" "+cs; } }
2.添加 Comparable 接口,重寫 compareTo 方法。而後你能夠用 TreeSet 結構進行排序。它會自動排序。數組
實例:數據結構
Bean:性能
package chc; public class StuVo implements Comparable<StuVo>{ private String id; private String name; private Integer age; public StuVo(String id, String name, Integer age) { this.id=id; this.name=name; this.age=age; } public int compareTo(StuVo stu) { return this.name.compareTo(stu.getName()); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
Demo:this
package chc; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class ArrayListSortDemo { public static void main(String[] args) { List<StuVo> stuList=new ArrayList<StuVo>(); StuVo stu=new StuVo("1","h小明",11); stuList.add(stu); stu=new StuVo("2","d阿熊",15); stuList.add(stu); stu=new StuVo("3","a張三",10); stuList.add(stu); stu=new StuVo("4","b李四",15); stuList.add(stu); Collections.sort(stuList); Iterator<StuVo> it =stuList.iterator(); while(it.hasNext()){ System.out.println(it.next().getName()); } } }
項目中的代碼:spa
對材料代號displayCode排序,該值是個數字字符串,很長,可能存在小數點,因此使用bigdecimal來判斷:code
index = 1; for(String key:endMap.keySet()){ pBudget0805 = endMap.get(key); pBudget0805.setIndex(index+""); data.add(pBudget0805); index++; }; Comparator<ProjectBudget0805> comparator = new Comparator<ProjectBudget0805>(){ public int compare(ProjectBudget0805 p1, ProjectBudget0805 p2) { return new BigDecimal(p2.getDisplayCode()).compareTo(new BigDecimal(p1.getDisplayCode())); } }; Collections.sort(data,comparator); index = 1; for(String key:endMap.keySet()){ pBudget0805 = endMap.get(key); pBudget0805.setIndex(index+""); data.add(pBudget0805); index++; }; Comparator<ProjectBudget0805> comparator = new Comparator<ProjectBudget0805>(){ public int compare(ProjectBudget0805 p1, ProjectBudget0805 p2) { return new BigDecimal(p2.getDisplayCode()).compareTo(new BigDecimal(p1.getDisplayCode())); } }; Collections.sort(data,comparator); //循環排序後的data,設置記錄正確的index,再放入到一個新的data1中 index = 1; for(ProjectBudget0805 p:data){ p.setIndex(index+""); data1.add(pBudget0805); index++; }; //data1就是咱們想要的結果
經過這個能夠發現list集合中是按照displayCode的升序來排列的.對象
可是這麼作有一個肯定,要循環兩次,才能得出記錄正確的index索引號,改良後的寫法:blog
index = 1; // for(String key:endMap.keySet()){ // pBudget0805 = endMap.get(key); // pBudget0805.setIndex(index+""); // data.add(pBudget0805); // index++; // }; List<Entry<String, ProjectBudget0805>> endList = new ArrayList<>(endMap.entrySet()); Comparator<Map.Entry<String, ProjectBudget0805>> comparator = new Comparator<Map.Entry<String, ProjectBudget0805>>(){ public int compare(Map.Entry<String, ProjectBudget0805> o1,Map.Entry<String, ProjectBudget0805> o2) { String p1 = o1.getValue().getDisplayCode(); String p2 = o2.getValue().getDisplayCode(); return new BigDecimal(p2).compareTo(new BigDecimal(p1)); } }; Collections.sort(endList,comparator); //這裏是先排序,而後設置正確的索引號,不須要建立新的data1對象來放置data中的內容 for(Entry<String, ProjectBudget0805> entity:endList){ pBudget0805 = entity.getValue(); pBudget0805.setIndex(index+""); data.add(pBudget0805); index++; }
附上一篇關於Map的文章:排序
java中的Map結構是key->value鍵值對存儲的,並且根據Map的特性,同一個Map中不存在兩個Key相同的元素,而value不存在這個限制。換句話說,在同一個Map中Key是惟一的,而value不惟一。Map是一個接口,咱們不能直接聲明一個Map類型的對象,在實際開發中,比較經常使用的Map性數據結構是HashMap和TreeMap,它們都是Map的直接子類。若是考慮到存取效率的話,建議使用HashMap數據結構,而若是須要考慮到Key的順序,建議使用TreeMap,可是TreeMap在刪除、添加過程當中須要排序,性能比較差。
1.以Key進行排序
//咱們能夠聲明一個TreeMap對象 Map<Integer, Person> map = new TreeMap<Integer, Person>(); //而後往map中添加元素,能夠經過輸出結果,能夠發現map裏面的元素都是排好序的 //遍歷集合 for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext();) { Person person = map.get(it.next()); System.out.println(person.getId_card() + " " + person.getName()); } //咱們也能夠聲明一個HashMap對象,而後把HashMap對象賦值給TreeMap,以下: Map<Integer, Person> map = new HashMap<Integer, Person>(); TreeMap treemap = new TreeMap(map);
2.以Value進行排序:
//先聲明一個HashMap對象: Map<String, Integer> map = new HashMap<String, Integer>(); //而後咱們能夠將Map集合轉換成List集合中,而List使用ArrayList來實現以下: List<Entry<String,Integer>> list = new ArrayList<Entry<String,Integer>>(map.entrySet()); //最後經過Collections.sort(List l, Comparator c)方法來進行排序,代碼以下: Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return (o2.getValue() - o1.getValue()); } });
上述代碼是講map中的value按照逆序排序,若是須要按照升序進行排序的話,只須要修改o2.getValue() - o1.getValue()爲o1.getValue() - o2.getValue()便可
在排序規則中也能夠加上另外的條件,例如:
Comparator<ProjectBudget0708_1> comparator = new Comparator<ProjectBudget0708_1>(){ public int compare(ProjectBudget0708_1 p1, ProjectBudget0708_1 p2) { BigDecimal index1 = p1.getIndex2(); BigDecimal index2 = p2.getIndex2(); //若是被比較的兩個數都包含小數點(即定額下掛靠的消耗的排序) if(index1.toString().contains(".") && index2.toString().contains(".")){ String str1 = index1.toString(); String str2 = index2.toString(); String subStr1 = str1.substring(0, str1.indexOf(".")); String subStr2 = str2.substring(0, str2.indexOf(".")); //若是subStr1等於subStr2,表示兩個消耗在同一個定額下,那麼根據兩個消耗的displayCode排序;其他的狀況都是經過index2進行排序 if(subStr1.equals(subStr2)){ return new BigDecimal(p1.getListCode()).compareTo(new BigDecimal(p2.getListCode())); } }; return p1.getIndex2().compareTo(p2.getIndex2()); } }; Collections.sort(data,comparator);
另外再說個例子:
好比有兩個list每一個list中的元素是同種類型,都爲object[]數組,如今要把這兩個list組合到一塊兒,按照object中的某個屬性按照順序顯示,
方法1.能夠循環每一個list,而後添加兩個list中的元素,在添加的時候注意排除掉已經添加過的元素便可,最後用compare比較器定義比較規則,最後按照規則進行排序.
方法2.利用treeSet,treeSet是有序的,不能用hashSet
TreeSet<Object[]> set = new TreeSet<Object[]>(new Comparator<Object[]>(){ public int compare(Object[] p1, Object[] p2) { int num = ((BigInteger)p1[16]).compareTo((BigInteger)p2[16]); return num; } }); String secUid = ""; if(list != null){ for(Object[] object:list){ secUid = (String)object[0]; set.add(object); } } if(list1 != null){ for(Object[] object:list1){ secUid = (String)object[0]; set.add(object); } }
在treeSet添加元素的時候,就已經按照裏面object[]數組中的某個元素的值排好序了