Java Iterable Sort isSorted(Iterable iterable, final Comparator cmp)

Here you can find the source of isSorted(Iterable iterable, final Comparator cmp)

Description

is Sorted

License

Open Source License

Declaration

public static <T> boolean isSorted(Iterable<T> iterable, final Comparator<T> cmp) 

Method Source Code

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

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

public class Main {
    public static <T> boolean isSorted(Iterable<T> iterable, final Comparator<T> cmp) {
        Iterator<T> iter = iterable.iterator();

        if (!iter.hasNext()) {
            return true;
        }//from   w  ww.  ja  v a  2 s  .com

        T t = iter.next();
        while (iter.hasNext()) {
            T t2 = iter.next();
            if (cmp.compare(t, t2) > 0)
                return false;
            t = t2;
        }

        return true;
    }
}

Related

  1. isSorted(Iterable iterable)
  2. sort(Iterable things, Comparator comparator)
  3. sortAscending(Iterable elements)
  4. sorted(Iterable items)