Collections對List集合中的數據進行排序 有時候需要對集合中的元素按照一定的規則進行排序,這就需要用到 Java中提供的對集合進行操作的工具類Collections,其中的sort方法 先看一個簡單的例子: reference: https://blog.csdn.net/veryisj ...
Collections對List集合中的數據進行排序
有時候需要對集合中的元素按照一定的規則進行排序,這就需要用到
Java中提供的對集合進行操作的工具類Collections,其中的sort方法
No1.先看一個簡單的例子:
1 public static void main(String[] args) { 2 List<Integer> nums = new ArrayList<Integer>(); 3 nums.add(2); 4 nums.add(9); 5 nums.add(7); 6 nums.add(1); 7 nums.add(0); 8 System.out.println(nums); 9 Collections.sort(nums); 10 System.out.println(nums); 11 }
結果如下:
[2, 9, 7, 1, 0]
[0, 1, 2, 7, 9]
建立一個No2代碼要用的entity,如下:
1 import java.util.Date; 2 3 public class Record implements Comparable<Record> { 4 private String name; 5 private int age; 6 private Date start; 7 8 public Record(String name, int age, Date start) { 9 this.name = name; 10 this.age = age; 11 this.start = start; 12 } 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public int getAge() { 23 return age; 24 } 25 26 public void setAge(int age) { 27 this.age = age; 28 } 29 30 public Date getStart() { 31 return start; 32 } 33 34 public void setStart(Date start) { 35 this.start = start; 36 } 37 38 @Override 39 public int compareTo(Record o) { 40 int nameIndex = this.name.compareTo(o.name); 41 int ageIndex = 0; 42 int startIndex = 0; 43 // 姓名一樣則比較年齡 44 if (nameIndex == 0) { 45 ageIndex = this.age - o.age; 46 } 47 // 年齡一樣則比較開始時間 48 if (ageIndex == 0) { 49 boolean isAfter = this.start.after(o.start); 50 if (isAfter) { 51 startIndex = 1; 52 } else { 53 startIndex = -1; 54 } 55 } 56 return nameIndex+ageIndex+startIndex; 57 } 58 59 }
No2.稍複雜一些的排序:
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.List; 6 7 public class ListOrder { 8 public static void main(String[] args) throws ParseException { 9 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 10 List<Record> records = new ArrayList<Record>(); 11 records.add(new Record("a", 2, df.parse("2015-12-16"))); 12 records.add(new Record("d", 8, df.parse("2015-12-16"))); 13 records.add(new Record("a", 1, df.parse("2019-12-16"))); 14 records.add(new Record("x", 0, df.parse("2014-12-16"))); 15 records.add(new Record("a", 1, df.parse("2018-12-16"))); 16 for (Record record : records) { 17 System.out.println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart()); 18 } 19 Collections.sort(records); 20 System.out.println("--------------------------------------"); 21 for (Record record : records) { 22 System.out.println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart()); 23 } 24 } 25 }
輸出結果:
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
--------------------------------------
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
Collections提供的第二種排序方法sort(List<T> list, Comparator<? super T> c)
所使用的entity:
1 import java.util.Date; 2 3 public class Record { 4 private String name; 5 private int age; 6 private Date start; 7 8 public Record(String name, int age, Date start) { 9 this.name = name; 10 this.age = age; 11 this.start = start; 12 } 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public int getAge() { 23 return age; 24 } 25 26 public void setAge(int age) { 27 this.age = age; 28 } 29 30 public Date getStart() { 31 return start; 32 } 33 34 public void setStart(Date start) { 35 this.start = start; 36 } 37 }
No3:代碼:
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.List; 7 8 public class ListOrder { 9 10 public static void main(String[] args) throws ParseException { 11 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 12 List<Record> records = new ArrayList<Record>(); 13 records.add(new Record("a", 2, df.parse("2015-12-16"))); 14 records.add(new Record("d", 8, df.parse("2015-12-16"))); 15 records.add(new Record("a", 1, df.parse("2019-12-16"))); 16 records.add(new Record("x", 0, df.parse("2014-12-16"))); 17 records.add(new Record("a", 1, df.parse("2018-12-16"))); 18 for (Record record : records) { 19 System.out 20 .println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart()); 21 } 22 Collections.sort(records, new Comparator<Record>() { 23 @Override 24 public int compare(Record r1, Record r2) { 25 int nameIndex = r1.getName().compareTo(r2.getName()); 26 int ageIndex = 0; 27 int startIndex = 0; 28 // 姓名一樣則比較年齡 29 if (nameIndex == 0) { 30 ageIndex = r1.getAge() - r2.getAge(); 31 } 32 // 年齡一樣則比較開始時間 33 if (ageIndex == 0) { 34 boolean isAfter = r1.getStart().after(r2.getStart()); 35 if (isAfter) { 36 startIndex = 1; 37 } else { 38 startIndex = -1; 39 } 40 } 41 return nameIndex + ageIndex + startIndex; 42 } 43 }); 44 System.out.println("--------------------------------------"); 45 for (Record record : records) { 46 System.out 47 .println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart()); 48 } 49 } 50 }
結果:
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
--------------------------------------
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
reference:
https://blog.csdn.net/veryisjava/article/details/51675036
https://www.jb51.net/article/72284.htm
https://blog.csdn.net/u012901117/article/details/76853113