How can java customize sort of sorting in List for date sorting

  • 2021-12-11 07:34:01
  • OfStack

Directory java Custom sort () Sort 1, Problem Description 2, Solution 3 in List, Other java Collections Classes: sort () Ascending Sort Forward Sort

java customizes sort () sorting in List

1. Problem description

List is an ordered and repeatable set in java, which comes with its own. sort () sorting method, which is very effective when sorting the pure number type List set. However, for loading other types of List sets, it is difficult for us to control the sorting of the sort () method, such as the sorting of a date set.

2. Solutions

List in java allows us to customize the sort () sorting method, and the following customizes the sort sorting method of List collection, which is used to sort a date set of string type.


// Collection to be sorted 
List<String> list=new ArrayList<String>();
list.add("2019-06");
list.add("2019-11");
list.add("2019-02");
list.add("2019-09");
list.add("2019-05");
// Customize list Sort, aggregate data ( Month ) Sort in ascending order ;
final SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM");
Collections.sort(list, new Comparator<String>(){
    @Override
 public int compare(String month1, String month2) {
  int mark = 1;
  try {
      Date date1 = sdft.parse(month1);
      Date date2 = sdft.parse(month2);
      if(date1.getTime() < date2.getTime()){
       mark = -1;// Adjustment sequence ,-1 Do not need to adjust the order ;
      }
      if(month1.equals(month2)){
       mark =  0;
  }
  } catch (ParseException e) {
   LOG.error(" Date conversion exception ", e);
   e.printStackTrace();
  }
 return mark;
 } //compare
});

3. Others

In addition, java objects of two date types can also be compared in the following methods.


Date() date1=new Date();
Date() date2=new SimpleDateFormat("yyyy-MM-dd").parse("2019-06-11");
Boolean flag;
if(date1.before(date2)){
 flag=true;
}

a. before (b); This method determines whether the a date is less than the b date, and returns a Boolean result.

java Collections class: sort () ascending sort

Forward sort

The static method sort () of the Collections class allows you to sort the elements in the collection in ascending order. This requires that all elements in the list must implement the Comparable interface and that all elements must be comparable with each other using the specified comparator.

The sort () method mainly has the following two overloaded forms

void sort(List list) Sorts the elements in the collection in ascending order according to their natural order. void sort(List list,Comparator comparator) Sorts the elements in the collection in the sort specified by the comparator parameter.

public  class  Test{
    public static void main(String[] args) {
        Student Student =new Student(" Zhang 1","6M");
        Student Student1 =new Student(" Zhang 2","1M");
        Student Student2 =new Student(" Zhang 4","5M");
        Student Student3 =new Student(" Zhang 5","1M");
        List<Student> list=new ArrayList<Student>();
        list.add(Student);
        list.add(Student1);
        list.add(Student2);
        list.add(Student3);
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return  o2.getAge().compareTo(o1.getAge());
            }
        });
        System.out.println(list.toString());
    } 
}

Related articles: