1 package net.sourceforge.phpdt.internal.ui.util;
3 import java.util.Comparator;
5 import org.eclipse.jface.util.Assert;
8 * Quick sort to sort key-value pairs. The keys and arrays are specified in
11 public class TwoArrayQuickSorter {
13 private Comparator fComparator;
18 public static final class StringComparator implements Comparator {
19 private boolean fIgnoreCase;
21 StringComparator(boolean ignoreCase) {
22 fIgnoreCase = ignoreCase;
25 public int compare(Object left, Object right) {
26 return fIgnoreCase ? ((String) left)
27 .compareToIgnoreCase((String) right) : ((String) left)
28 .compareTo((String) right);
33 * Creates a sorter with default string comparator. The keys are assumed to
37 * specifies whether sorting is case sensitive or not.
39 public TwoArrayQuickSorter(boolean ignoreCase) {
40 fComparator = new StringComparator(ignoreCase);
44 * Creates a sorter with a comparator.
47 * the comparator to order the elements. The comparator must not
48 * be <code>null</code>.
50 public TwoArrayQuickSorter(Comparator comparator) {
51 fComparator = comparator;
55 * Sorts keys and values in parallel.
58 * the keys to use for sorting.
60 * the values associated with the keys.
62 public void sort(Object[] keys, Object[] values) {
63 if ((keys == null) || (values == null)) {
64 Assert.isTrue(false, "Either keys or values in null"); //$NON-NLS-1$
71 internalSort(keys, values, 0, keys.length - 1);
74 private void internalSort(Object[] keys, Object[] values, int left,
76 int original_left = left;
77 int original_right = right;
79 Object mid = keys[(left + right) / 2];
81 while (fComparator.compare(keys[left], mid) < 0)
84 while (fComparator.compare(mid, keys[right]) < 0)
88 swap(keys, left, right);
89 swap(values, left, right);
93 } while (left <= right);
95 if (original_left < right)
96 internalSort(keys, values, original_left, right);
98 if (left < original_right)
99 internalSort(keys, values, left, original_right);
103 * Swaps x[a] with x[b].
105 private static final void swap(Object x[], int a, int b) {