Example usage for java.util Comparator compare

List of usage examples for java.util Comparator compare

Introduction

In this page you can find the example usage for java.util Comparator compare.

Prototype

int compare(T o1, T o2);

Source Link

Document

Compares its two arguments for order.

Usage

From source file:Quicksort.java

private static int partition(Object[] a, int left, int right, int pivotIndex, Comparator c) {
    int leftIndex = left;
    int rightIndex = right - 1;
    while (true) {
        while (c.compare(a[++leftIndex], a[pivotIndex]) < 0)
            ;/* ww  w.j  av  a  2s .  com*/
        while (c.compare(a[--rightIndex], a[pivotIndex]) > 0)
            ;
        if (leftIndex >= rightIndex) {
            break; // pointers cross so partition done
        } else {
            swap(a, leftIndex, rightIndex);
        }
    }
    swap(a, leftIndex, right - 1); // restore pivot
    return leftIndex; // return pivot location
}

From source file:AnimationTester.java

private static <E> void merge(E[] a, int from, int mid, int to, Comparator<? super E> comp) {
    int n = to - from + 1;
    Object[] values = new Object[n];

    int fromValue = from;

    int middleValue = mid + 1;

    int index = 0;

    while (fromValue <= mid && middleValue <= to) {
        if (comp.compare(a[fromValue], a[middleValue]) < 0) {
            values[index] = a[fromValue];
            fromValue++;//from w  w  w .ja  va2  s. c  om
        } else {
            values[index] = a[middleValue];
            middleValue++;
        }
        index++;
    }

    while (fromValue <= mid) {
        values[index] = a[fromValue];
        fromValue++;
        index++;
    }
    while (middleValue <= to) {
        values[index] = a[middleValue];
        middleValue++;
        index++;
    }

    for (index = 0; index < n; index++)
        a[from + index] = (E) values[index];
}

From source file:Main.java

public static Object findObject(Collection<?> c, Object o, Comparator cmp) {
    if (c != null && o != null && cmp != null) {
        Iterator<?> it = c.iterator();
        while (it.hasNext()) {
            Object current = it.next();
            if (o.getClass().equals(current.getClass())) {
                if (cmp.compare(current, o) == 0) {
                    return current;
                }//ww w . j  a va  2s . co m
            }
        }
    }
    return null;
}

From source file:edu.msu.cme.rdp.alignment.pairwise.PairwiseKNN.java

private static <T> void insert(T n, List<T> list, Comparator<T> comp, int k) {
    int i = list.size();
    list.add(n);//from www.  ja v  a  2 s. com

    while (i > 0 && comp.compare(list.get(i), list.get(i - 1)) > 0) {
        Collections.swap(list, i, i - 1);
        i--;
    }

    if (list.size() > k) {
        list.remove(k);
    }
}

From source file:FastMergeSort.java

@SuppressWarnings({ "unchecked" })
private static void mergeSort(Object src[], Object dest[], int low, int high, int off, Comparator c) {
    int length = high - low;

    // use insertion sort on smallest arrays
    if (length < 7) {
        for (int i = low; i < high; i++) {
            for (int j = i; j > low && c.compare(dest[j - 1], dest[j]) > 0; j--) {
                Object temp = dest[j];
                dest[j] = dest[j - 1];/*from   w w  w  .j  a  v  a 2  s. c o  m*/
                dest[j - 1] = temp;
            }
        }
        return;
    }

    // recursively sort halves of dest into src
    int destLow = low;
    int destHigh = high;
    low += off;
    high += off;
    int mid = (low + high) >> 1;
    mergeSort(dest, src, low, mid, -off, c);
    mergeSort(dest, src, mid, high, -off, c);

    // is list already sorted?
    if (c.compare(src[mid - 1], src[mid]) <= 0) {
        System.arraycopy(src, low, dest, destLow, length);
        return;
    }

    // merge sorted halves from src into dest
    for (int i = destLow, p = low, q = mid; i < destHigh; i++) {
        if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0) {
            dest[i] = src[p++];
        } else {
            dest[i] = src[q++];
        }
    }
}

From source file:Main.java

/**
 *
 * @param <T>// www  . j  a v a 2 s  .  c o  m
 * @param list
 * @param element
 * @param start
 * @param end
 * @param c
 * @return the position of the element if present, -1 otherwise
 */
public static <T extends Comparable<? super T>> int binarySearchOf(List<T> list, T element, int start, int end,
        Comparator<? super T> c) {
    int mid = (start + end) / 2;
    if (start > end || mid >= end) {
        return -1;
    }
    int compared = -1;
    try {
        compared = c == null ? list.get(mid).compareTo(element) : c.compare(list.get(mid), element);
    } catch (Exception e) {
        throw new IllegalStateException(
                "Exception while searching for " + element + " from " + start + " to " + end, e);
    }

    if (compared == 0) {
        return mid;
    } else if (compared < 0) {
        return binarySearchOf(list, element, mid + 1, end, c);
    } else {
        return binarySearchOf(list, element, start, mid, c);
    }
}

From source file:Main.java

public static <T> int aArrCompare(T[] aArrA, T[] aArrB, Comparator<T> cmptor) {
    int lenA = aArrA.length;
    int lenB = aArrB.length;
    if (lenA < lenB) {
        return -1;
    } else if (lenA > lenB) {
        return 1;
    } else {/*w w  w . j  a  v  a 2s .c  o m*/
        for (int i = 0; i < lenA; ++i) {
            int res = cmptor.compare(aArrA[i], aArrB[i]);
            if (res != 0) {
                return res;
            }
        }
        return 0;
    }
}

From source file:Main.java

/**
 * Get the "best" in collection. That is the least if direction is < 0, otherwise the greatest. The first is chosen if there are multiples.
 * @param <T>//  ww w  .j a  v  a2s. co m
 * @param <U>
 * @param c
 * @param comp
 * @param direction
 * @return
 */
public static <T, U extends Collection<T>> T getBest(U c, Comparator<T> comp, int direction) {
    Iterator<T> it = c.iterator();
    if (!it.hasNext())
        return null;
    T bestSoFar = it.next();
    if (direction < 0) {
        while (it.hasNext()) {
            T item = it.next();
            int compValue = comp.compare(item, bestSoFar);
            if (compValue < 0) {
                bestSoFar = item;
            }
        }
    } else {
        while (it.hasNext()) {
            T item = it.next();
            int compValue = comp.compare(item, bestSoFar);
            if (compValue > 0) {
                bestSoFar = item;
            }
        }
    }
    return bestSoFar;
}

From source file:Quicksort.java

/**
 * Sort the entries in a between left and right inclusive.
 *//*from  w w  w .j  a  v a  2s . c  om*/
public static void quicksort(Object[] a, int left, int right, Comparator c) {
    int size = right - left + 1;
    switch (size) {
    case 0:
    case 1:
        break;
    case 2:
        if (c.compare(a[left], a[right]) > 0)
            swap(a, left, right);
        break;
    case 3:
        if (c.compare(a[left], a[right - 1]) > 0)
            swap(a, left, right - 1);
        if (c.compare(a[left], a[right]) > 0)
            swap(a, left, right);
        if (c.compare(a[left + 1], a[right]) > 0)
            swap(a, left + 1, right);
        break;
    default:
        int median = median(a, left, right, c);
        int partition = partition(a, left, right, median, c);
        quicksort(a, left, partition - 1, c);
        quicksort(a, partition + 1, right, c);
    }
}

From source file:de.tudarmstadt.lt.utilities.ListUtils.java

public static <T> int[] sortIdsByValue(final List<T> values, final Comparator<T> comparator) {
    Integer[] ids = new Integer[values.size()];
    for (int id = 0; id < ids.length; ids[id] = id++)
        ;//w  w w .  ja v a  2  s  .  c om
    Arrays.sort(ids, new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return comparator.compare(values.get(o1), values.get(o2));
        }
    });
    return org.apache.commons.lang.ArrayUtils.toPrimitive(ids);
}