1 package net.sourceforge.phpdt.internal.ui.util;
3 import java.util.Comparator;
6 //import org.eclipse.jface.text.Assert;
7 import org.eclipse.core.runtime.Assert;
10 * Quick sort to sort key-value pairs. The keys and arrays are specified in
13 public class TwoArrayQuickSorter {
15 private Comparator fComparator;
20 public static final class StringComparator implements Comparator {
21 private boolean fIgnoreCase;
23 StringComparator(boolean ignoreCase) {
24 fIgnoreCase = ignoreCase;
27 public int compare(Object left, Object right) {
28 return fIgnoreCase ? ((String) left)
29 .compareToIgnoreCase((String) right) : ((String) left)
30 .compareTo((String) right);
35 * Creates a sorter with default string comparator. The keys are assumed to
39 * specifies whether sorting is case sensitive or not.
41 // public TwoArrayQuickSorter(boolean ignoreCase) {
42 // fComparator = new StringComparator(ignoreCase);
46 * Creates a sorter with a comparator.
49 * the comparator to order the elements. The comparator must not
50 * be <code>null</code>.
52 public TwoArrayQuickSorter(Comparator comparator) {
53 fComparator = comparator;
57 * Sorts keys and values in parallel.
60 * the keys to use for sorting.
62 * the values associated with the keys.
64 public void sort(Object[] keys, Object[] values) {
65 if ((keys == null) || (values == null)) {
66 Assert.isTrue(false, "Either keys or values in null"); //$NON-NLS-1$
73 internalSort(keys, values, 0, keys.length - 1);
76 private void internalSort(Object[] keys, Object[] values, int left,
78 int original_left = left;
79 int original_right = right;
81 Object mid = keys[(left + right) / 2];
83 while (fComparator.compare(keys[left], mid) < 0)
86 while (fComparator.compare(mid, keys[right]) < 0)
90 swap(keys, left, right);
91 swap(values, left, right);
95 } while (left <= right);
97 if (original_left < right)
98 internalSort(keys, values, original_left, right);
100 if (left < original_right)
101 internalSort(keys, values, left, original_right);
105 * Swaps x[a] with x[b].
107 private static final void swap(Object x[], int a, int b) {