Java List Sort isSorted(List list)

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

Description

Determine if the given list is sorted

License

Open Source License

Parameter

Parameter Description
list the list

Return

true iff the list is sorted

Declaration

@SuppressWarnings("unchecked")
public static boolean isSorted(List<? extends Comparable> list) 

Method Source Code

//package com.java2s;
/*/*from ww w . j a  va2 s.  com*/
 * Copyright (c) 2010 The Jackson Laboratory
 * 
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.List;

public class Main {
    /**
     * Determine if the given list is sorted
     * @see java.util.Collections#sort(List)
     * @param list
     *          the list
     * @return
     *          true iff the list is sorted
     */
    @SuppressWarnings("unchecked")
    public static boolean isSorted(List<? extends Comparable> list) {
        Comparable prevItem = null;
        for (Comparable currItem : list) {
            if (prevItem != null && currItem.compareTo(prevItem) < 0) {
                return false;
            }

            prevItem = currItem;
        }

        return true;
    }

    /**
     * Determine if the given array is sorted
     * @param values
     *          the values
     * @return
     *          true iff the values are sorted
     */
    public static boolean isSorted(long[] values) {
        for (int i = 1; i < values.length; i++) {
            if (values[i - 1] > values[i]) {
                return false;
            }
        }

        return true;
    }

    /**
     * Determine if the given array is sorted
     * @param values
     *          the values
     * @return
     *          true iff the values are sorted
     */
    public static boolean isSorted(float[] values) {
        for (int i = 1; i < values.length; i++) {
            if (values[i - 1] > values[i]) {
                return false;
            }
        }

        return true;
    }

    /**
     * Determine if the given array is sorted
     * @param values
     *          the values
     * @return
     *          true iff the values are sorted
     */
    public static boolean isSorted(double[] values) {
        for (int i = 1; i < values.length; i++) {
            if (values[i - 1] > values[i]) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. getSortedStringList(List pathList)
  2. getZipfPlotData(List sortedCount)
  3. interpolateLinearlyQuantile(List sortedDataAscendingOrder, Double p)
  4. isEqualList(final List list1, final List list2, Comparator c, boolean sortList)
  5. isSorted(Iterable list)
  6. isSorted(List list)
  7. isSorted(List> items, boolean asc)
  8. isSorted(List list, Comparator c)
  9. isSorted(List list)