Java List Sort isSorted(List list, Comparator c)

Here you can find the source of isSorted(List list, Comparator c)

Description

Checks that the list is sorted

License

Open Source License

Parameter

Parameter Description
list a parameter
c a parameter

Declaration

public static <E> boolean isSorted(List<E> list, Comparator<? super E> c) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class Main {
    /**//from w w  w.  j  av  a  2  s .  c o m
     * Checks that the list is sorted
     * @param list
     * @param c
     * @return
     */
    public static <E> boolean isSorted(List<E> list, Comparator<? super E> c) {
        if (list.isEmpty())
            return true;

        Iterator<E> i = list.iterator();

        E lastR = i.next();

        while (i.hasNext()) {
            E r = i.next();

            if (c.compare(lastR, r) > 0)
                return false;
        }

        return true;
    }
}

Related

  1. isEqualList(final List list1, final List list2, Comparator c, boolean sortList)
  2. isSorted(Iterable list)
  3. isSorted(List list)
  4. isSorted(List list)
  5. isSorted(List> items, boolean asc)
  6. isSorted(List list)
  7. isSorted(List list)
  8. isSorted(List list)
  9. isSortedDescending(List list)