Share this page 

Sort an arrayTag(s): Language Varia


JDK1.4+
case-sensitive
String[] myArray = new String[] {"foo","bar","baz"};
java.util.Arrays.sort(myArray);
case-insensitive
String[] myArray = new String[] {"foo","Bar","baz"};
java.util.Arrays.sort(myArray, java.text.Collator.getInstance());
less than JDK1.4
[ArraySorter.java]
public class ArraySorter {
  /*
  ** Sort in the same array
  */
  public static void sort(Object[] a, Comparer comparer) {
    sort(a, null, 0, a.length - 1, true, comparer);
  }

  /*
  ** Sort a and b, using a as the reference
  */
  public static void sort(Object[] a, Object[] b,
    int from, int to, boolean ascending, Comparer comparer) {
    // No sort
    if (a == null || a.length < 2) return;

    // sort using Quicksort
    int i = from, j = to;
    Object center = a[ (from + to) / 2 ];
    do {
      if (ascending) {
        while( (i < to) && (comparer.compare(  center, a[i]) > 0) )
            i++;
        while( (j > from) && (comparer.compare(center, a[j]) < 0) )
            j--;
        }
      else {
        // Decending sort
        while( (i < to) && (comparer.compare(  center, a[i]) < 0) )
           i++;
        while( (j > from) && (comparer.compare(center, a[j]) > 0) )
           j--;
        }
      if (i < j) {
        // Swap elements
        Object temp = a[i]; a[i] = a[j]; a[j] = temp;
        // Swap in b array if needed
        if (b != null) {
          temp = b[i]; b[i] = b[j]; b[j] = temp;
        }
      }
      if (i <= j) { i++; j--; }
      } while(i <= j);
    // Sort the rest
    if (from < j) sort(a, b, from, j, ascending, comparer);
    if (i < to) sort(a, b, i, to, ascending, comparer);
  }

public static interface Comparer {
  /**
   * The interface implementation should compare the two
   * objects and return an int using these rules:
   * if (a > b)  return > 0;
   * if (a == b) return 0;
   * if (a < b)  return < 0;
   */
   public int compare(Object a, Object b);
   }
}

[TestArraySorter.java]
public class TestArraySorter {
  public static final ASCIIComparer asciiComparer = new ASCIIComparer();
  public static void main(String args[]) {
    if (args.length == 0)
      System.out.println("give me some args to sort");
    else {
      ArraySorter.sort(args, asciiComparer);
      for (int i = 0; i < args.length; i++) {
        System.out.println(args[i]);
      }
    }
  }

  public static class ASCIIComparer implements ArraySorter.Comparer {
    public int compare(Object a, Object b) {
      return ((String)a).compareTo((String)b);
    }
  }
}