List and Comparators : Comparator « Collections Data Structure « Java






List and Comparators

    
import java.util.*;

public class CompTest {
  public static void main(String args[]) {
    ArrayList u2 = new ArrayList();
    u2.add("Beautiful Day");
    u2.add("Stuck In A Moment You Can't Get Out Of");
    u2.add("Elevation");
    u2.add("Walk On");
    u2.add("Kite");
    u2.add("In A Little While");
    u2.add("Wild Honey");
    u2.add("Peace On Earth");
    u2.add("When I Look At The World");
    u2.add("New York");
    u2.add("Grace");

    Comparator comp = Comparators.stringComparator();
    Collections.sort(u2, comp);
    System.out.println(u2);

    Arrays.sort(args, comp);
    System.out.print("[");
    for (int i=0, n=args.length; i<n; i++) {
      if (i != 0) System.out.print(", ");
      System.out.print(args[i]);
    }
    System.out.println("]");
  }
}
class Comparators {

  public static Comparator stringComparator() {
    return new Comparator() {

      public int compare(Object o1, Object o2) {
        String s1 = (String)o1;
        String s2 = (String)o2;
        int len1 = s1.length();
        int len2 = s2.length();
        int n = Math.min(len1, len2);
        char v1[] = s1.toCharArray();
        char v2[] = s2.toCharArray();
        int pos = 0;

        while (n-- != 0) {
          char c1 = v1[pos];
          char c2 = v2[pos];
          if (c1 != c2) {
            return c1 - c2;
          }
          pos++;
        }
        return len1 - len2;
      }
    };
  }

  public static Comparator integerComparator() {
    return new Comparator() {

      public int compare(Object o1, Object o2) {
        int val1 = ((Integer)o1).intValue();
        int val2 = ((Integer)o2).intValue();
        return (val1<val2 ? -1 : (val1==val2 ? 0 : 1));
      }
    };
  }

  public static Comparator dateComparator() {
    return new Comparator() {

      public int compare(Object o1, Object o2) {
        long val1 = ((Date)o1).getTime();
        long val2 = ((Date)o2).getTime();
        return (val1<val2 ? -1 : (val1==val2 ? 0 : 1));
      }
    };
  }
}


           
         
    
    
    
  








Related examples in the same category

1.Creating a Comparable objectCreating a Comparable object
2. Writing Your own Comparator Writing Your own Comparator
3.A Class Implementing Comparable
4.Comparator for comparing strings ignoring first character
5.Sort backwards
6.Company and Employee
7.Search with a Comparator
8.Keep upper and lowercase letters togetherKeep upper and lowercase letters together
9.Uses anonymous inner classesUses anonymous inner classes
10.Building the anonymous inner class in-placeBuilding the anonymous inner class in-place
11.Sort an array of strings in reverse order.
12.Sort an array of strings, ignore case difference.
13.Comparator uses a Collator to determine the proper, case-insensitive lexicographical ordering of two strings.
14.Using the Comparable interface to compare and sort objects
15.Sort on many(more than one) fields
16.File Name Comparator
17.Comparator similar to String.CASE_INSENSITIVE_ORDER, but handles only ASCII characters
18.Natural Order Comparator
19.Reverse Order Comparator
20.A Comparator for Boolean objects that can sort either true or false first
21.Invertible Comparator
22.This program animates a sort algorithm