Java List Sort isSorted(List list)

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

Description

Returns whether a list is strictly sorted.

License

Open Source License

Parameter

Parameter Description
list List

Return

whether list is sorted

Declaration

public static <T> boolean isSorted(List<T> list) 

Method Source Code

//package com.java2s;
/*//from w w  w  .  j av  a2  s . c om
 // This software is subject to the terms of the Eclipse Public License v1.0
 // Agreement, available at the following URL:
 // http://www.eclipse.org/legal/epl-v10.html.
 // You must accept the terms of that agreement to use this software.
 //
 // Copyright (C) 2001-2005 Julian Hyde
 // Copyright (C) 2005-2012 Pentaho and others
 // All Rights Reserved.
 */

import java.util.*;

public class Main {
    /**
     * Returns whether a list is strictly sorted.
     *
     * @param list List
     * @return whether list is sorted
     */
    public static <T> boolean isSorted(List<T> list) {
        T prev = null;
        for (T t : list) {
            if (prev != null && ((Comparable<T>) prev).compareTo(t) >= 0) {
                return false;
            }
            prev = t;
        }
        return true;
    }
}

Related

  1. isSorted(List list)
  2. isSorted(List list)
  3. isSorted(List> items, boolean asc)
  4. isSorted(List list, Comparator c)
  5. isSorted(List list)
  6. isSorted(List list)
  7. isSortedDescending(List list)
  8. isStringListSortedDesc(List list)
  9. isUnsortedEventsMatch(List actual, List expected)