Here you can find the source of isSorted(List extends Comparable> list)
Parameter | Description |
---|---|
list | the list |
@SuppressWarnings("unchecked") public static boolean isSorted(List<? extends Comparable> list)
//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; } }