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:Main.java

private static <E> E getSmallestNotNull(final Comparator<? super E> comparator,
        final List<? extends E> randomAccessList) {
    E result = randomAccessList.get(0);/*from   w  ww . j a v  a 2s  .c  om*/
    E element;

    for (int i = 1; i < randomAccessList.size(); i++) {
        element = randomAccessList.get(i);
        if (element == null) {
            continue;
        }
        if ((result == null) || (comparator.compare(element, result) < 0)) {
            result = element;
        }
    }

    if (result == null) {
        throw new NoSuchElementException();
    }

    return result;
}

From source file:Main.java

private static <E> E getGreatestNotNull(final Comparator<? super E> comparator,
        final List<? extends E> randomAccessList) {
    E result = randomAccessList.get(0);//from w w w .  ja  v a  2 s . c  om
    E element;

    for (int i = 1; i < randomAccessList.size(); i++) {
        element = randomAccessList.get(i);
        if (element == null) {
            continue;
        }
        if ((result == null) || (comparator.compare(element, result) > 0)) {
            result = element;
        }
    }

    if (result == null) {
        throw new NoSuchElementException();
    }

    return result;
}

From source file:ml.shifu.shifu.util.QuickSort.java

private static <T> void quicksort(List<T> values, Comparator<T> comparator, int low, int high) {
    int i = low, j = high;
    // get the pivot element from the middle of the list
    T pivot = values.get(low + (high - low) / 2);

    // divide into two parts by pivot
    while (i <= j) {
        // from the left to find the first element that greater than pivot
        while (comparator.compare(pivot, values.get(i)) > 0) {
            i++;//from w ww .j  a  v a2s.c  o m
        }

        // form the right to find the first element that less than pivot
        while (comparator.compare(pivot, values.get(j)) < 0) {
            j--;
        }

        // Wowo, we found those two elements, exchange them
        if (i <= j) {
            exchange(values, i, j);
            i++;
            j--;
        }
    }

    // have data on the left, sort it
    if (low < j) {
        quicksort(values, comparator, low, j);
    }

    // have data on the right, sort it
    if (i < high) {
        quicksort(values, comparator, i, high);
    }
}

From source file:Main.java

/**
 * Group Data into {@link List}.//from w ww. j a v a  2s  .  c om
 * @param <T> Type of object
 * @param list {@link List}
 * @param sortBy {@link Comparator}
 * @param groupBy {@link Comparator}
 * @return {@link List}
 */
public static <T> List<List<T>> groupBy(final List<T> list, final Comparator<T> sortBy,
        final Comparator<T> groupBy) {

    T prev = null;
    List<T> currentlist = new ArrayList<>();
    List<List<T>> results = new ArrayList<>();
    results.add(currentlist);

    List<T> newlist = new ArrayList<>(list);
    Collections.sort(newlist, sortBy);

    Iterator<T> itr = newlist.iterator();
    while (itr.hasNext()) {

        T curr = itr.next();

        if (prev != null) {
            int result = groupBy.compare(curr, prev);

            if (result != 0) {
                currentlist = new ArrayList<>();
                results.add(currentlist);
            }
        }

        currentlist.add(curr);

        prev = curr;
    }

    return results;
}

From source file:org.gradle.util.GUtil.java

public static <T> Comparator<T> last(final Comparator<? super T> comparator, final T lastValue) {
    return new Comparator<T>() {
        public int compare(T o1, T o2) {
            boolean o1Last = comparator.compare(o1, lastValue) == 0;
            boolean o2Last = comparator.compare(o2, lastValue) == 0;
            if (o1Last && o2Last) {
                return 0;
            }// ww  w  .j  av  a  2s . c o  m
            if (o1Last && !o2Last) {
                return 1;
            }
            if (!o1Last && o2Last) {
                return -1;
            }
            return comparator.compare(o1, o2);
        }
    };
}

From source file:org.openvpms.web.component.im.util.IMObjectSorter.java

/**
 * Returns a comparator that sorts objects on name.
 * <p/>/*from  ww  w .j av a 2s.  c o  m*/
 * This comparator handles nulls.
 *
 * @param ascending if <tt>true</tt> sort in ascending order; otherwise sort in descending order
 * @return a new comparator
 */
@SuppressWarnings("unchecked")
public static <T extends IMObject> Comparator<T> getNameComparator(boolean ascending) {
    final Comparator comparator = getComparator(ascending);
    return new Comparator<T>() {
        public int compare(T o1, T o2) {
            return comparator.compare(o1.getName(), o2.getName());
        }
    };
}

From source file:FastQuickSort.java

@SuppressWarnings({ "unchecked" })
public static void qsort(Object[] c, Comparator comparator) {
    int i, j, left = 0, right = c.length - 1, stack_pointer = -1;
    int[] stack = new int[128];
    Object swap, temp;//from  ww w  . ja  va 2s. c o m
    while (true) {
        if (right - left <= 7) {
            for (j = left + 1; j <= right; j++) {
                swap = c[j];
                i = j - 1;
                while (i >= left && comparator.compare(c[i], swap) > 0) {
                    c[i + 1] = c[i--];
                }
                c[i + 1] = swap;
            }
            if (stack_pointer == -1) {
                break;
            }
            right = stack[stack_pointer--];
            left = stack[stack_pointer--];
        } else {
            int median = (left + right) >> 1;
            i = left + 1;
            j = right;
            swap = c[median];
            c[median] = c[i];
            c[i] = swap;
            if (comparator.compare(c[left], c[right]) > 0) {
                swap = c[left];
                c[left] = c[right];
                c[right] = swap;
            }
            if (comparator.compare(c[i], c[right]) > 0) {
                swap = c[i];
                c[i] = c[right];
                c[right] = swap;
            }
            if (comparator.compare(c[left], c[i]) > 0) {
                swap = c[left];
                c[left] = c[i];
                c[i] = swap;
            }
            temp = c[i];
            while (true) {
                //noinspection ControlFlowStatementWithoutBraces,StatementWithEmptyBody
                while (comparator.compare(c[++i], temp) < 0)
                    ;
                //noinspection ControlFlowStatementWithoutBraces,StatementWithEmptyBody
                while (comparator.compare(c[--j], temp) > 0)
                    ;
                if (j < i) {
                    break;
                }
                swap = c[i];
                c[i] = c[j];
                c[j] = swap;
            }
            c[left + 1] = c[j];
            c[j] = temp;
            if (right - i + 1 >= j - left) {
                stack[++stack_pointer] = i;
                stack[++stack_pointer] = right;
                right = j - 1;
            } else {
                stack[++stack_pointer] = left;
                stack[++stack_pointer] = j - 1;
                left = i;
            }
        }
    }
}

From source file:Main.java

public static <T> int compareAsSet(Comparator<? super T> elementComparator, Collection<? extends T> list1,
        Collection<? extends T> list2) {
    int res = Ints.compare(list1.size(), list2.size());
    if (res != 0)
        return res;

    SortedSet<? extends T> set1 = toSortedSet(elementComparator, list1);
    SortedSet<? extends T> set2 = toSortedSet(elementComparator, list2);

    Iterator<? extends T> elements2 = set2.iterator();
    for (T element1 : set1) {
        res = elementComparator.compare(element1, elements2.next());
        if (res != 0)
            return res;
    }//  www. j av a 2  s .  co  m
    return 0;
}

From source file:net.darkmist.clf.Util.java

public static <T> T[] merge(T[] a, T[] b, T[] dest, Comparator<T> comparator) {
    int ai, bi, di;
    int dlen = a.length + b.length;

    if (dest.length < dlen)
        throw new IllegalArgumentException("Destination array is not large enough");

    // check for simple case where one whole array goes before the other
    if (comparator.compare(a[a.length - 1], b[0]) <= 0)
        return concat(a, b, dest);
    if (comparator.compare(b[b.length - 1], a[0]) <= 0)
        return concat(b, a, dest);

    // binarySearch for the start of one in the other
    if (comparator.compare(a[0], b[0]) <= 0) { // a starts, figure out where b[0] is 
        ai = Arrays.binarySearch(a, b[0], comparator);
        if (ai < 0) // ai = (-(insersion_point)-1)
            ai = -(ai + 1);/*from ww w .  jav  a2  s.  c  o m*/
        System.arraycopy(a, 0, dest, 0, ai);
        di = ai;
        bi = 0;
    } else { // b starts, figure out where a[0] is
        bi = Arrays.binarySearch(b, a[0], comparator);
        if (bi < 0) // bi = (-(insersion_point)-1)
            bi = -(bi + 1);
        System.arraycopy(b, 0, dest, 0, bi);
        di = bi;
        ai = 0;
    }
    if (logger.isDebugEnabled())
        logger.debug("ai=" + ai + " bi=" + bi + " di=" + di);

    // merge until we run out of one
    for (; ai < a.length && bi < b.length; di++) {
        if (comparator.compare(a[ai], b[bi]) <= 0)
            dest[di] = a[ai++];
        else
            dest[di] = b[bi++];
    }

    // one array has been used up here...copy the rest
    if (ai < a.length)
        System.arraycopy(a, ai, dest, di, a.length - ai);
    else
        System.arraycopy(b, bi, dest, di, b.length - bi);

    return dest;
}

From source file:org.splandroid.tr.Main.java

/**
 * Remove test cases from the list of test-cases. This allows only certain
 * test cases to be run in a test suite. The comparator defines which test
 * case is to be removed./*from  w  w  w. j a v a2  s .  co  m*/
 * 
 * @param testCases
 * @param testCaseIds
 * @param comparator
 */
private static void removeTestCaseDescriptors(List<ITestCaseDescriptor> testCases,
        Collection<String> testCaseIds, Comparator comparator) {
    final Iterator<ITestCaseDescriptor> iterator = testCases.iterator();

    while (iterator.hasNext()) {
        final ITestCaseDescriptor testCase = iterator.next();
        final String currentTestCaseId = testCase.getId();
        if (comparator.compare(currentTestCaseId, testCaseIds) == 0) {
            logger.info(String.format("Removing test case with ID [%s]", testCase.getId()));
            iterator.remove();
        }
    }
}