Java Programming Tips: List sorting

Source: Internet
Author: User
Tags collator static class first row

The list implementation defined in the Java Collection Framework has vector,arraylist and LinkedList. These collections provide index access to the group of objects. They provide support for adding and removing elements. However, they do not have built-in element ordering support.

You can sort the list element by using the sort () method in the Java.util.Collections class. You can either pass a list object to the method or pass a list and a comparator. If the elements in the list are all classes of the same type, and the class implements the comparable interface, you can simply invoke Collections.sort (). If this class does not implement comparator, you can also pass a comparator to the method sort () and sort. If you do not want to use the default sorting order, you can also pass a comparator to the method sort () to sort. If the elements in the list are not all of the same type of class, you are not so lucky when sorting. Unless you write a dedicated cross class comparator.

What sort of order? If the element is a string object, the sort order of the province is based on the character encoding, which is essentially the ascii/unicode value of each character. If strictly restricted in the processing of English, but the provincial sort order is usually sufficient, because it first row a A-Z, then the lowercase letter A-Z. However, if you are dealing with non-English characters, or if you just want to use a different sort order, then there is a second change in Collections.sort (). For example, you want to sort by using the inverse sequence of strings. To implement this function, you can obtain a reverse sequence comparator in the collections class by Reverseorder (). Then you pass the reverse sequence comparator to the sort () method. In other words, you are doing the following:

List list = ...;
Comparator comp = Collections.reverseOrder();
Collections.sort(list, comp);

If the list contains items: Man, man, Woman, and Woman, the sorted list will be man, Woman, Mans, Woman. There's nothing complicated here. A very important point to note is that Collections.sort () is in-place sorting. If you need to keep the original order, you need to copy the original set first, in sort, like this:

List list = ...;
List copyOfList = new ArrayList(list);
Collections.sort(copyOfList);

Here, the list of sorted lists is: Man, Woman, man, Woman, but the original list (man, man, Woman, Woman) is reserved.

So far, the sort is case-sensitive. How do you do the sort of case-insensitive? One way to implement this is to implement comparator like this:

public static class CaseInsensitiveComparator
implements Comparator {
public int compare(Object element1,
Object element2) {
String lower1 =
element1.toString().toLowerCase();
String lower2 =
element2.toString().toLowerCase();
return lower1.compareTo(lower2);
}
}

You really don't need to create this class by hand. Instead, you can be a comparator,case_insensivtive_order for existence, which is defined in the string class.

There is a little bit of a problem with this approach. The sort () algorithm provides a stable sort and retains the same elements as the original sequence. This means that a list of two elements "woman" and "woman" will have a different sort, which is determined by the order in which two elements appear in the list.

What about the difference in language? The Java.text package provides collector and Collectionkey classes for language-sensitive sorting. Here is an example:

Note that if your text is local, not the default, you need to pass a local language to the getinstance () method, like this:

public static class CollatorComparator
implements Comparator {
Collator collator = Collator.getInstance();
public int compare(Object element1,
Object element2) {
CollationKey key1 = collator.getCollationKey(
element1.toString());
CollationKey key2 = collator.getCollationKey(
element2.toString());
return key1.compareTo(key2);
}
}

Related Article

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.