Java实现对List中的对象按某个字段进行排序
声明
1)该文章整理自网上的大牛和专家无私奉献的资料,具体引用的资料请看参考文献。
2)本文仅供学术交流,非商用。如果某部分不小心侵犯了大家的利益,还望海涵,并联系博主删除。
3)博主才疏学浅,文中如有不当之处,请各位指出,共同进步,谢谢。
4)此属于第一版本,若有错误,还需继续修正与增删。还望大家多多指点。大家都共享一点点,一起为祖国科研的推进添砖加瓦。
1 public class Logs implements Serializable, Comparable<Logs> { 2 3 private String id; 4 private String username; 5 private String contents; 6 private String consequence; 7 private String ip; 8 private Long times; 9 10 @Override 11 public int compareTo(Logs o) { 12 return 0; 13 } 14 15 } 16 解析: Logs实体,要继承Comparable<T>接口, 然后再实现一个public int compareTo(Logs o) 方法. 17 18 现在,我要对List<Logs> 中的数据按 times 字段进行排序 19 20 21 @Test 22 public void testMath(){ 23 List<Logs> list = new ArrayList<Logs>(); 24 list.add(new Logs("1","chen", "djifand", "jdifa", "jdfid", 49L)); 25 list.add(new Logs("134","ce", "cae", "jdifa", "jdfid", 123L)); 26 list.add(new Logs("3","cet", "djifand", "jdifa", "jdfid", 99L)); 27 list.add(new Logs("4","egge", "djifand", "jdifa", "jdfid", 111L)); 28 list.add(new Logs("15","chen", "djifand", "jdifa", "jdfid", 59L)); 29 list.add(new Logs("6","chen2", "cae", "jdifa", "jdfid", 123L)); 30 list.add(new Logs("87","cet", "djifand", "jdifa", "jdfid", 213L)); 31 list.add(new Logs("8","egge", "djifand", "jdifa", "jdfid", 201L)); 32 list.add(new Logs("9","egge2", "djifand", "jdifa", "jdfid", 269L)); 33 list.add(new Logs("10","ce", "cae", "jdifa", "jdfid", 121L)); 34 list.add(new Logs("11","chen3", "djifand", "jdifa", "jdfid", 1123L)); 35 list.add(new Logs("5","cet2", "djifand", "jdifa", "jdfid", 12L)); 36 37 System.out.println("---- 排序前的输出 ----- "); 38 for (Logs logs : list) { 39 System.out.println("数据: "+logs); 40 } 41 System.out.println("---- 排序后的结果 ----- "); 42 List<Logs> search = search(list); 43 44 for (Logs logs : search) { 45 System.out.println("-->"+logs); 46 } 47 48 } 49 50 51 // 方式1: 降序排列 52 public List<Logs> search(List<Logs> logsList){ 53 Collections.sort(logsList, new Comparator<Logs>() { 54 @Override 55 public int compare(Logs o1, Logs o2) { 56 if ((o1.getTimes() > o2.getTimes())){ 57 return -1; 58 } 59 if (o1.getTimes() == o2.getTimes()){ 60 return 0; 61 } 62 return 1; 63 } 64 }); 65 return logsList; 66 } 67 68 // 方式2: 升序排列 69 public List<Logs> search(List<Logs> logsList){ 70 Collections.sort(logsList, new Comparator<Logs>() { 71 @Override 72 public int compare(Logs o1, Logs o2) { 73 if ((o1.getTimes() > o2.getTimes())){ 74 return 1; 75 } 76 if (o1.getTimes() == o2.getTimes()){ 77 return 0; 78 } 79 return -1; 80 } 81 }); 82 return logsList; 83 } 84 // 方式3: 降序排列 85 public List<Logs> search(List<Logs> logsList){ 86 Collections.sort(logsList, new Comparator<Logs>() { 87 @Override 88 public int compare(Logs o1, Logs o2) { 89 return -o1.getTimes().compareTo(o2.getTimes()); 90 } 91 }); 92 return logsList; 93 } 94 95 // 方式4: 升序排列 96 public List<Logs> search(List<Logs> logsList){ 97 Collections.sort(logsList, new Comparator<Logs>() { 98 @Override 99 public int compare(Logs o1, Logs o2) { 100 return o1.getTimes().compareTo(o2.getTimes()); 101 } 102 }); 103 return logsList; 104 }