Java Collections. sort () implements the default List sorting methods and custom methods [convert], collections. sort sorting
1. default list sorting method provided by java
Main Code:
List <String> list = new ArrayList (); list. add ("Liu Yuanyuan ");
List. add ("Wang Shuo ");
List. add ("Li Ming ");
List. add ("Liu Di ");
List. add ("Liu Bu ");
// Ascending
Collections. sort (list, Collator. getInstance (java. util. locale. CHINA); // note: the sorting is based on the pinyin letters of Chinese characters, rather than the general sorting method of Chinese characters.
For (int I = 0; I <list. size (); I ++)
{
System. out. print (list. get (I ));
}
System. out. println ("");
// Descending order
Collections. reverse (list); // if no sorting rule is specified, it is also sorted by letters.
For (int I = 0; I <list. size (); I ++)
{
System. out. print (list. get (I ));
}
Output result:
Li Ming, Liu Bu, Liu Di, Liu xiaoyuan, Wang Shuo
Wang Shuo Liu Yuanyuan Liu Di Liu Bu Li Ming
2. Custom sorting rules:
The first method is to implement the Comparable interface of the model class, and rewrite the int compareTo (Object o) method.
Model class:
Public class StudentDTO implements Comparable
{
Private String name;
Private int age;
Public String getName ()
{
Return name;
}
Public void setName (String name)
{
This. name = name;
}
Public ObjType getType ()
{
Return type;
}
Public void setAge (int age)
{
This. age = age;
}
@ Override
Public int compareTo (Object o)
{
StudentDTO sdto = (StudentDTO) o;
Int otherAge = sdto. getAge ();
// Note: enum-type's comparation depend on types 'list order of enum method
// So, if compared property is enum-type, then its comparationfollow ObjEnum. objType order
Return this. age. compareTo (otherAge );
}
}
Main method:
Public static void main (String [] args)
{
List <StudentDTO> studentList = new ArrayList ();
StudentDTO s1 = new StudentDTO ();
S. setName ("yuanyuan ");
S. setAge (22 );
StudentList. add (s1 );
StudentDTO s1 = new StudentDTO ();
S. setName ("lily ");
S. setAge (23 );
StudentList. add (s2 );
Collections. sort (studentList); // sort by age in ascending order of 22, 23,
Collections. reverse (studentList); // sort by age in descending order
}
The second is the Comparator interface implemented by the Comparator class, And the int compare (Object o1, Object o2) method is rewritten;
Model class:
Public class StudentDTO implements Comparable
{
Private String name;
Private int age;
Public String getName ()
{
Return name;
}
Public void setName (String name)
{
This. name = name;
}
Public ObjType getType ()
{
Return type;
}
Public void setAge (int age)
{
This. age = age;
}
}
Comparator class:
Class MyCompartor implements Comparator
{
@ Override
Public int compare (Object o1, Object o2)
{
StudentDTO sdto1 = (StudentDTO) o1;
StudentDTO sdto2 = (StudentDTO) o2;
Return sdto1.getAge. compareTo (stdo2.getAge ())
}
}
Main method:
Public static void main (String [] args)
{
List <StudentDTO> studentList = new ArrayList ();
StudentDTO s1 = new StudentDTO ();
S. setName ("yuanyuan ");
S. setAge (22 );
StudentList. add (s1 );
StudentDTO s1 = new StudentDTO ();
S. setName ("lily ");
S. setAge (23 );
StudentList. add (s2 );
MyComparetor mc = new MyComparetor ();
Collections. sort (studentList, mc); // sort by age in ascending order of 22, 23,
Collections. reverse (studentList, mc); // sort by age in descending order
}
Note:
1. the sorting method for arrays is as follows:
String [] names = {"Wang Lin", "Yang Bao", "Li Zhen", "Liu Di", "Liu Bo "};
Arrays. sort (names, com. ibm. icu. text. Collator. getInstance (com. ibm. icu. util. ULocale.SIMPLIFIED_CHINESE); // Ascending order;
System. out. println (Arrays. toString (names ));
2. Sorting Chinese characters: You can try ICU4J to get better results, especially when the last name is some uncommon words,
Use com. ibm. icu. text. Collator to replace java. text. Collator, and use com. ibm. icu. util. ULocale to replace java. util. Locale.
3. For enum1.compareTo (enum2) of the enumeration type, the values of the enumeration type are compared according to the sequence in which they are defined,
Instead of comparing values in alphabetical order.