Java Collections.sort () default methods and custom methods for list ordering

Source: Internet
Author: User
Tags collator comparable locale ming

Default list ordering method provided by 1.java

Main code:

list<string> list = new ArrayList (); List.add ("Liu Yuanyuan");

List.add ("Wang Shuo");
List.add ("Li Ming");
List.add ("abstract");
List.add ("Liu Bu");

Ascending
Collections.sort (List,collator.getinstance (Java.util.Locale.CHINA));//Note: is based on the alphabetical ordering of Chinese characters, rather than according to 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
Collections.reverse (list);//When you do not specify a collation, it is also sorted by letter
for (int i=0;i<list.size (); i++)
{
System.out.print (List.get (i));
}

Output Result:

Li Ming-liubliudi Liu Yuanyuan Wang Shuo
Wang Shuo Liu Yuanyuan Liu Diliu Briming

2. Custom collation:

The first is the model class that implements the comparable interface, overriding the overriding 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.)
{
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 was 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); According to age ascending 22, 23,

Collections.reverse (studentlist); By age descending 23,22

}

The second is that the comparator class implements the comparator interface, overriding the int compare (object O1, Object O2) 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.)
{
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); According to age ascending 22, 23,

Collections.reverse (STUDENTLIST,MC); By age descending 23,22

}

Note:

1. The sorting method for the array is as follows:

String[] names = {"Wang Lin", "Yang Pao", "Li Zhen Jizhou", "abstract", "Liu Bo"};
Arrays.sort (names, Com.ibm.icu.text.Collator.getInstance (Com.ibm.icu.util.ULocale. Simplified_chinese));//ascending;
System.out.println (arrays.tostring (names));

2. The ordering of Chinese characters: can try to use icu4j will get better results, especially when the surname is some uncommon words,

Replace Java.text.Collator with Com.ibm.icu.text.Collator, replace with Com.ibm.icu.util.ULocale Java.util.Locale

3. Enum1.compareto (ENUM2) for enumeration types is compared in the order in which enumerated type values are defined, and the later is larger,

Rather than by the alphabetical order of the values.

Java Collections.sort () default methods and custom methods for list ordering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.