Several ways to sort Java List

Table of contents

I. Introduction

Two, the text

1. Option 1

2. Scheme 2

3. Scheme 3

4. Solution 4

3. Summary

4. End


I. Introduction

Sometimes we need to sort the List, how to do it in Java? This article records several sorting methods for List in Java. I hope this article will be helpful to you.

Two, the text

1. Option 1

1.1. Simple sorting method, such as the following code

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class sort {
  public static void main(String[] args) {
      List<Integer> list = new ArrayList<Integer>();
      list.add(new Integer(3));
      list.add(new Integer(14));
      list.add(new Integer(6));
      list.add(new Integer(10));
       // 简单的int类似数据,可以使用Collections的sort方法即可完成排序
      Collections.sort(list);
      System.out.println(list.toString());
  }
}

The output is:

[3,6,10,14]

This simple sort will output directly as natural data.

2. Scheme 2

2.1. Relatively complicated sorting method of parameters, when the parameters are objects

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class sort {
  public static void main(String[] args) {
      List<User> list = new ArrayList<User>();
      list.add(new User("张三", 5));
      list.add(new User("李四", 30));
      list.add(new User("王五", 19));
      list.add(new User("王麻子", 17));
       // 按年龄排序
      Collections.sort(list); 
      System.out.println(list.toString());
  }
}


class User implements Comparable<User>{

  private String name; //姓名
  private int age; // 年龄

  public User(String name, int age) {
    this.name = name;
    this.age = age;
  }
  // getter && setter
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
  this.age = age;
  }
  @Override
  public String toString() {
    return "User [name=" + name + ", age=" + age + "]";
  }

  @Override
  public int compareTo(User user) {           
      // 重写Comparable接口的compareTo方法,根据年龄升序排列,降序修改相减顺序即可
    return this.age - user.getAge();
  }
}

Program results: sorted by age in ascending order

[User [name=张三, age=5], User [name=王麻子, age=17], User [name=王五, age=19], User [name=李四, age=30]]

3. Scheme 3

3.1. Use anonymous inner class to implement sorting

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class sort {
  public static void main(String[] args) {
      List<User> list = new ArrayList<User>();
      list.add(new User("张三", 5));
      list.add(new User("李四", 30));
      list.add(new User("王五", 19));
      list.add(new User("王麻子", 17));
      Collections.sort(list, new Comparator<User>() {
        @Override
        public int compare(User u1, User u2) {
          int diff = u1.getAge() - u2.getAge();
          if (diff > 0) {
            return 1;
          }else if (diff < 0) {
            return -1;
          }
          return 0; //相等为0
        }
      }); // 按年龄排序
      System.out.println(list.toString());
  }
}

The result of the operation is:

[User [name=张三, age=5], User [name=王麻子, age=17], User [name=王五, age=19], User [name=李四, age=30]]

4. Solution 4

4.1 is also the most brief solution, recommended by bloggers, and can be done with one line of code

import java.util.Collections;
import java.util.List;
public class sort {
    public static void main(String[] args) {
      List<User> list = new ArrayList<User>();
      list.add(new User("张三", 5));
      list.add(new User("李四", 30));
      list.add(new User("王五", 19));
      list.add(new User("王麻子", 17));
       // 这一行代码即可完成排序
      list.sort(Comparator.comparing(User::getAge));
      System.out.println(list.toString());
  }
}

The output is:

[User [name=张三, age=5], User [name=王麻子, age=17], User [name=王五, age=19], User [name=李四, age=30]]

3. Summary

The above are several methods commonly used for list sorting. The first two methods implement the Comparable interface and rewrite the compareTo method through the entity. The reason why the first method does not implement the interface and implement the compareTo method can be seen by viewing the source code of the Interger class. , java has already implemented it for us, so there is no need to write it again

The third method implements the compare method through the anonymous inner class to implement the Comparator interface, and implements sorting through a custom comparator.

4. End

If you think this article is helpful to you, you might as well like it and bookmark it, maybe you won’t be able to find it next time you need it.

Guess you like

Origin blog.csdn.net/Wjhsmart/article/details/115935693