/java/

Collections.sort and caseinsensitiveorder

2013-04-08 21:44:45

package sorttest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortTest {

    private static Comparator

<post> ComparatorPost = new Comparator<post>(){
       @Override
       public int compare(Post o1, Post o2)
       {
           //return o1.getName().compareTo( o2.getName() );
           return String.CASE_INSENSITIVE_ORDER.compare( o1.getName(), o2.getName() );
       }
    };

    public static void main(String[] args) {
        List<post> posts = new ArrayList<post>();

        posts.add(new Post("dLorem"));
        posts.add(new Post("DLorem"));
        posts.add(new Post("b1Lorem"));
        posts.add(new Post("B1Lorem"));
        posts.add(new Post("cLorem"));
        posts.add(new Post("$Lorem"));
        posts.add(new Post("1Lorem"));
        posts.add(new Post("CLorem"));
        posts.add(new Post("aLorem"));
        posts.add(new Post("ALorem"));

        for(Post post : posts){
            System.out.println("post="+post.getName());
        }

        System.out.println("========================");

        Collections.sort(posts, ComparatorPost);

        for(Post post : posts){
            System.out.println("post="+post.getName());
        }
    }
}