How to rewrite the method of Java List to implement ascending, descending, and reverse order

This article mainly introduces the case of Java List sort() method rewriting compare() to achieve ascending, descending, and reverse order. It has good reference value and I hope it will be helpful to everyone. Let's follow the editor to take a look

This article aims to realize the ascending, descending, and reverse sorting of the List by rewriting the compare() method of the Comparator interface.

First, make it clear: In
compare(Integer o1, Integer o2){}, o1 represents the next element in the List container, and o2 represents the previous element in the List container!

This can be clearly understood through the following examples:

public static void main(String[] args) {
    
     
 List<Integer> list = new ArrayList<Integer>();
 list.add(1);
 list.add(2);
 list.add(3); 
 list.sort(new Comparator<Integer>() {
    
    
 public int compare(Integer o1, Integer o2) {
    
    
 System.out.println(o1 + "," + o2);//输出o1,o2
 return 0;
 }
 });
 }

The output is:

  • 2,1

  • 3,2

Ascending
code:

public static void main(String[] args) {
    
     
 List<Integer> list = new ArrayList<Integer>();
 list.add(1);
 list.add(2);
 list.add(3); 
 list.sort(new Comparator<Integer>() {
    
    
 public int compare(Integer o1, Integer o2) {
    
    
 if(o1>o2)
 return 1;//第二个元素(o1)比第一个元素(o2)大,返回1
 if(o1==o2)
 return 0;
 return -1;
 }//1,0,-1三者同时出现时,1表示不交换位置,0表示相等时不交换,-1表示交换
 }); 
 System.out.println(list.toString());
 }

Output:

[1,2,3]

Descending
code:

public static void main(String[] args) {
    
     
 List<Integer> list = new ArrayList<Integer>();
 list.add(1);
 list.add(2);
 list.add(3); 
 list.sort(new Comparator<Integer>() {
    
    
 public int compare(Integer o1, Integer o2) {
    
    
 if(o1>o2)
 return -1;//第二个元素(o1)比第一个元素(o2)大,返回-1
 if(o1==o2)
 return 0;
 return 1;
 }//1,0,-1三者同时出现时,1表示不交换位置,0表示相等时不交换,-1表示交换
 }); 
 System.out.println(list.toString());

Output:

[3,2,1]

Reverse
code:

public static void main(String[] args) {
    
     
 List<Integer> list = new ArrayList<Integer>();
 list.add(1);
 list.add(2);
 list.add(3); 
 list.sort(new Comparator<Integer>() {
    
    
 public int compare(Integer o1, Integer o2) {
    
    
 return -1;
 }//倒序就直接返回-1
 });
 System.out.println(list.toString());
 }

Output:

[3,2,1]

Supplement: Sample code for ordering, reverse order, and random sorting of elements in the List collection in Java

I won't talk too much nonsense, everyone should look at the code directly~

import java.util.Collections;
import java.util.LinkedList;
import java.util.List; 
public class Test {
    
     
 List list = new LinkedList();
 public static void main(String[] args) {
    
    
 List list = new LinkedList();
  for ( int i = 0 ; i < 9 ; i ++ ) {
    
    
  list.add( " a " + i);
 } 
 Collections.sort(list); // 顺序排列 
 System.out.println(list);
  
 Collections.shuffle(list); // 混乱的意思 
 System.out.println(list);
  
 Collections.reverse(list); // 倒序排列 
 System.out.println(list);
  
 System.out.println(Collections.binarySearch(list, " a5 " )); // 折半查找 
 } 
}

Supplement: java8 is sorted according to two fields (one positive order and one reverse)

List<Student> collect2 = list.stream()
 .sorted(Comparator.comparing(Student::getAge).reversed().thenComparing(Student::getScore))
 .collect(Collectors.toList());

The above is personal experience, I hope to give you a reference, and I hope you can support the editor. If there are mistakes or not fully considered, please feel free to enlighten me

Guess you like

Origin blog.csdn.net/dcj19980805/article/details/115214235