Sort arrays
For example, there is an integer array:
Int [] intArray = new int [] {4, 1, 3,-23 };
How do we sort data? Are you thinking about a fast sorting algorithm at this time? Let's take a look at the following implementation methods:
Import java. util .*;
Public class Sort {
Public static void main (String [] args ){
Int [] intArray = new int [] {4, 1, 3,-23 };
Arrays. sort (intArray );
}
}
In this way, we use the static method sort () of Arrays to sort intArray in ascending order. Now the array has become {-23,1, 3,4 }.
If it is a character array:
String [] strArray = new String [] {"z", "a", "C "};
We use:
Arrays. sort (strArray );
After sorting, {C, a, z}, and sort () are sorted in ascending order based on the natural order of the elements. If you want to be case insensitive, write as follows:
Arrays. sort (strArray, String. CASE_INSENSITIVE_ORDER );
Of course, we can also specify a part of the array for sorting. For example, we want to sort the Part 0-2 of the array table (assuming the array length is greater than 3), and the other part remains unchanged. We can use:
Arrays. sort (strArray, 0, 2 );
In this way, we only sort the first three elements without affecting the subsequent parts.
Of course, some people may think, how do I sort in descending order? One of the many sort methods
Sort (T [] a, Comparator <? Super T> c)
We can use Comparator to obtain a reverse Comparator. Comparator will explain it later. The previous intArray [] is used as an example:
Arrays. sort (intArray, Comparator. reverseOrder ());
In this way, the result is {4, 3, 1,-23 }. If you do not want to modify the original code, you can also use:
Collections. reverse (Arrays. asList (intArray ));
Returns the reverse order of the array. The result is 4, 3, 1,-23 }.
The current situation has changed. Our array is no longer an array of the basic data type (primtive type) or String type, but an array of objects. The natural order of this array is unknown, so we need to implement the Comparable interface for this class. For example, we have a Name class:
Class Name implements Comparable <Name> {public String firstName, lastName; public Name (String firstName, String lastName) {this. firstName = firstName; this. lastName = lastName;} public int compareTo (Name o) {// implementation interface int lastCmp = lastName. compareTo (o. lastName); return (lastCmp! = 0? LastCmp: firstName. compareTo (o. firstName);} public String toString () {// Easy output Test return firstName + "" + lastName ;}}
In this way, when we sort this object array, we will first compare the lastName, then compare the firstName, and then get the order of the two objects, just like compareTo (Name o). Try it with a program:
Import java. util .*;
Public class NameSort {
Public static void main (String [] args ){
Name nameArray [] = {
New Name ("John", "Lennon "),
New Name ("Karl", "Marx "),
New Name ("Groucho", "Marx "),
New Name ("Oscar", "Grouch ")
};
Arrays. sort (nameArray );
For (int I = 0; I <nameArray. length; I ++ ){
System. out. println (nameArray [I]. toString ());
}
}
}
The results are as expected:
Oscar Grouch
John Lennon
Groucho Marx
Karl Marx
Sort the collection framework
If Arrays. sort () is understood to sort Arrays, the use of the collection framework is similar. Only replace Arrays with Collections. Note that Collections is a class and Collection is an interface. Although there is only one "s" difference, their meanings are completely different.
Suppose there is such a linked list:
Jsonlist list = new jsonlist ();
List. add (4 );
List. add (34 );
List. add (22 );
List. add (2 );
We only need to use:
Collections. sort (list );
You can sort the elements in ll in the ascending order, and the result is:
[2, 4, 22, 34]
If the element in the sort list is String, the basic data type is also sorted from small to large.
If you want to implement reverse sorting, that is, small sorting:
Collections. sort (list, Collectons. reverseOrder ());
If the elements in the sorted list are custom objects, you can implement the Comparable interface like the Name object above, so that Collection. sort () can be sorted for you.
If you want to sort an object according to your own ideas, you can use
Sort (List list, Comparator c)
Before providing an example, we need to describe the use of Comparator. The format of the Comparable interface is as follows:
Public interface Comparator <T> {
Int compare (T o1, T o2 );
}
In fact, the write method of int compare (T o1, T o2) in Comparator is similar to that of compareTo () in Comparable. In the above Name class, our comparison started from LastName. This is a habit of western people. In China, we want to start from fristName and do not want to modify the original code. At this time, comparator can be used:
Final Comparator <Name> FIRST_NAME_ORDER = new Comparator <Name> (){
Public int compare (Name n1, Name n2 ){
Int firstCmp = n1.firstName. compareTo (n2.firstName );
Return (firstCmp! = 0? FirstCmp: n1.lastName. compareTo
(N2.firstName ));
}
};
So that we can write a custom Comparator FIRST_NAME_ORDER.
Convert the name array in the previous example to List:
List <Name> list = Arrays. asList (nameArray );
Collections. sort (list, FIRST_NAME_ORDER );
Import java. util .*;
Public class NameSort {
Public static void main (String [] args ){
Name nameArray [] = {
New Name ("John", "Lennon "),
New Name ("Karl", "Marx "),
New Name ("Groucho", "Marx "),
New Name ("Oscar", "Grouch ")
};
Arrays. sort (nameArray );
For (int I = 0; I <nameArray. length; I ++ ){
System. out. println (nameArray [I]. toString ());
}
}
}
The results are as expected:
Oscar Grouch
John Lennon
Groucho Marx
Karl Marx
Sort the collection framework
If Arrays. sort () is understood to sort Arrays, the use of the collection framework is similar. Only replace Arrays with Collections. Note that Collections is a class and Collection is an interface. Although there is only one "s" difference, their meanings are completely different.
Suppose there is such a linked list:
Jsonlist list = new jsonlist ();
List. add (4 );
List. add (34 );
List. add (22 );
List. add (2 );
We only need to use:
Collections. sort (list );
You can sort the elements in ll in the ascending order, and the result is:
[2, 4, 22, 34]
If the element in the sort list is String, the basic data type is also sorted from small to large.
If you want to implement reverse sorting, that is, small sorting:
Collections. sort (list, Collectons. reverseOrder ());
If the elements in the sorted list are custom objects, you can implement the Comparable interface like the Name object above, so that Collection. sort () can be sorted for you.
If you want to sort an object according to your own ideas, you can use
Sort (List list, Comparator c)
Before providing an example, we need to describe the use of Comparator. The format of the Comparable interface is as follows:
Public interface Comparator <T> {
Int compare (T o1, T o2 );
}
In fact, the write method of int compare (T o1, T o2) in Comparator is similar to that of compareTo () in Comparable. In the above Name class, our comparison started from LastName. This is a habit of western people. In China, we want to start from fristName and do not want to modify the original code. At this time, comparator can be used:
Final Comparator <Name> FIRST_NAME_ORDER = new Comparator <Name> (){
Public int compare (Name n1, Name n2 ){
Int firstCmp = n1.firstName. compareTo (n2.firstName );
Return (firstCmp! = 0? FirstCmp: n1.lastName. compareTo
(N2.firstName ));
}
};
Set ORDER.
Convert the name array in the previous example to List:
List <Name> list = Arrays. asList (nameArray );
Collections. sort (list, FIRST_NAME_ORDER );