Java List's sort of method rewrites the case of compare of to realize ascending descending and reverse order
- 2021-08-31 08:06:18
- OfStack
The purpose of this paper is to realize the ascending, descending and reverse ordering of List by overwriting the compare () method of Comparator interface.
First of all, make one point clear:
In compare (Integer o1, Integer o2) {}, o1 represents the last element in the List container and o2 represents the first element in the List container!
This one point can be clearly understood through the following example:
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);// Output o1,o2
return 0;
}
});
}
The output is:
2, 1
3, 2
Ascending order
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;// No. 1 2 Elements ( o1 ) 1 Elements ( o2 ) Large, return 1
if(o1==o2)
return 0;
return -1;
}//1,0,-13 When people appear at the same time, 1 Indicates that positions are not exchanged, 0 Indicates no exchange when equal, -1 Representation exchange
});
System.out.println(list.toString());
}
Output:
[1,2,3]
Descending order
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;// No. 1 2 Elements ( o1 ) 1 Elements ( o2 ) Large, return -1
if(o1==o2)
return 0;
return 1;
}//1,0,-13 When people appear at the same time, 1 Indicates that positions are not exchanged, 0 Indicates no exchange when equal, -1 Representation exchange
});
System.out.println(list.toString());
Output:
[3,2,1]
Reverse order
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;
}// Go back directly in reverse order -1
});
System.out.println(list.toString());
}
Output:
[3,2,1]
Supplement: Java in the List set of elements in order, reverse order, random sort sample code
I won't talk too much, let's just look at the code ~
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); // Sequential arrangement
System.out.println(list);
Collections.shuffle(list); // The meaning of confusion
System.out.println(list);
Collections.reverse(list); // Reverse order
System.out.println(list);
System.out.println(Collections.binarySearch(list, " a5 " )); // Fold half search
}
}
Supplement: java8 sorts according to two fields (1 positive order and 1 flashback)
List<Student> collect2 = list.stream()
.sorted(Comparator.comparing(Student::getAge).reversed().thenComparing(Student::getScore))
.collect(Collectors.toList());