1 package net.sourceforge.phpdt.internal.ui.util;
3 import java.util.Comparator;
4 import org.eclipse.jface.util.Assert;
7 * Quick sort to sort key-value pairs. The keys and arrays are specified
10 public class TwoArrayQuickSorter {
12 private Comparator fComparator;
17 public static final class StringComparator implements Comparator {
18 private boolean fIgnoreCase;
20 StringComparator(boolean ignoreCase) {
21 fIgnoreCase= ignoreCase;
24 public int compare(Object left, Object right) {
26 ? ((String) left).compareToIgnoreCase((String) right)
27 : ((String) left).compareTo((String) right);
32 * Creates a sorter with default string comparator.
33 * The keys are assumed to be strings.
34 * @param ignoreCase specifies whether sorting is case sensitive or not.
36 public TwoArrayQuickSorter(boolean ignoreCase) {
37 fComparator= new StringComparator(ignoreCase);
41 * Creates a sorter with a comparator.
42 * @param comparator the comparator to order the elements. The comparator must not be <code>null</code>.
44 public TwoArrayQuickSorter(Comparator comparator) {
45 fComparator= comparator;
49 * Sorts keys and values in parallel.
50 * @param keys the keys to use for sorting.
51 * @param values the values associated with the keys.
53 public void sort(Object[] keys, Object[] values) {
54 if ((keys == null) || (values == null)) {
55 Assert.isTrue(false, "Either keys or values in null"); //$NON-NLS-1$
62 internalSort(keys, values, 0, keys.length - 1);
65 private void internalSort(Object[] keys, Object[] values, int left, int right) {
66 int original_left= left;
67 int original_right= right;
69 Object mid= keys[(left + right) / 2];
71 while (fComparator.compare(keys[left], mid) < 0)
74 while (fComparator.compare(mid, keys[right]) < 0)
78 swap(keys, left, right);
79 swap(values, left, right);
83 } while (left <= right);
85 if (original_left < right)
86 internalSort(keys , values, original_left, right);
88 if (left < original_right)
89 internalSort(keys, values, left, original_right);
93 * Swaps x[a] with x[b].
95 private static final void swap(Object x[], int a, int b) {