How do I sort a list of two methods using the collections.sort method from a Java collection

  • 2020-04-01 04:17:01
  • OfStack

The first is that the object in the list implements the Comparable interface That is as follows:



public class User implements Comparable
   <user>
   {
  private String name;
  private Integer order;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Integer getOrder() {
    return order;
  }
  public void setOrder(Integer order) {
    this.order = order;
  }
  public int compareTo(User arg0) {
    return this.getOrder().compareTo(arg0.getOrder());
  }
}
   </user>

Test it out:


public class Test{
  public static void main(String[] args) {
    User user1 = new User();
    user1.setName("a");
    user1.setOrder(1);
    User user2 = new User();
    user2.setName("b");
    user2.setOrder(2);
    List
   <user>
    list = new ArrayList
   <user>
    ();
    //Add user2 and add user1 here
    list.add(user2);
    list.add(user1);
    Collections.sort(list);
    for(User u : list){
      System.out.println(u.getName());
    }
  }
}
   </user>
   </user>

The output is as follows

a.
b

The second method is implemented according to the collections.sort overload method , such as:



public class User { //There is no need to implement the Comparable interface
  private String name;
  private Integer order;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Integer getOrder() {
    return order;
  }
  public void setOrder(Integer order) {
    this.order = order;
  }
}

The main class can be written like this:


public class Test{
  public static void main(String[] args) {
    User user1 = new User();
    user1.setName("a");
    user1.setOrder(1);
    User user2 = new User();
    user2.setName("b");
    user2.setOrder(2);
    List
   <user>
    list = new ArrayList
   <user>
    ();
    list.add(user2);
    list.add(user1);
    Collections.sort(list,new Comparator
    <user>
    (){
      public int compare(User arg0, User arg1) {
        return arg0.getOrder().compareTo(arg1.getOrder());
      }
    });
    for(User u : list){
      System.out.println(u.getName());
    }
  }
}
    </user>
   </user>
   </user>

The output is as follows

a.
b

The former is simple but can only be sorted by fixed attributes, while the latter is flexible and can specify sort items temporarily, but the code is not concise enough

Multi-field situations:


Collections.sort(list,new Comparator
   <user>
    (){
      public int compare(User arg0, User arg1) {
 //The first time was professional
        int i = arg0.getOrder().compareTo(arg1.getOrder());
//If the major is the same, then make a second comparison
      if(i==0){
//Second comparison
        int j=arg0.getXXX().compareTo(arg1.getXXX());
//Returns a sort by age if the length of schooling is the same
        if(j==0){
          return arg0.getCCC().compareTo(arg1.getCCC());
        }
        return j;
      }
      return i;
      }
    });
   </user>

That's what this site is about, and I hope it will be helpful to show you how to sort a list (both ways) using the collections.sort method from a Java collection.


Related articles: