Java Iterable Sort isSorted(Iterable iterable)

Here you can find the source of isSorted(Iterable iterable)

Description

From http://stackoverflow.com/questions/3047051/how-to-determine-if-a-list-is-sorted-in-java

License

Apache License

Parameter

Parameter Description
iterable a parameter
T a parameter

Declaration

public static <T extends Comparable<? super T>> boolean isSorted(Iterable<T> iterable) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**/*from  www  .  j a va2 s .c  om*/
     * From http://stackoverflow.com/questions/3047051/how-to-determine-if-a-list-is-sorted-in-java
     * @param iterable
     * @param <T>
     * @return
     */
    public static <T extends Comparable<? super T>> boolean isSorted(Iterable<T> iterable) {
        Iterator<T> iter = iterable.iterator();
        if (!iter.hasNext()) {
            return true;
        }
        T t = iter.next();
        while (iter.hasNext()) {
            T t2 = iter.next();
            if (t.compareTo(t2) > 0) {
                return false;
            }
            t = t2;
        }
        return true;
    }
}

Related

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