Re: Verifying a list is alphabetized
On Wed, 30 Nov 2011 07:52:49 -0800 (PST), laredotornado
<laredotornado@zipmail.com> wrote, quoted or indirectly quoted someone
who said :
I'm using Java 1.6. Given a java.util.List of Strings, what is the
quickest way to verify that the list is sorted in ascending order?
/*
* [InOrder.java]
*
* Summary: Are arrays/collections already in order?.
*
* Copyright: (c) 2011 Roedy Green, Canadian Mind Products,
http://mindprod.com
*
* Licence: This software may be copied and used freely for any
purpose but military.
* http://mindprod.com/contact/nonmil.html
*
* Requires: JDK 1.5+
*
* Created with: JetBrains IntelliJ IDEA IDE
http://www.jetbrains.com/idea/
*
* Version History:
* 1.0 2011-11-07 initial version
*/
package com.mindprod.common15;
import java.util.Comparator;
import java.util.List;
/**
* Are arrays/collections already in order?.
* <p/>
* Generics for these methods were cannibalised from Arrays.sort and
Collections.sort.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2011-11-07 initial version
* @since 2011-11-07
*/
public final class InOrder
{
// -------------------------- PUBLIC STATIC METHODS
--------------------------
/**
* Is this array already in order according to its Comparable
interface?
* Generics and arrays don't get along well. We get unchecked
warnings here.
* I don't know how to fix them. It may not be possible.
*
* @param array array of Comparable objects.
*
* @return true if array already in order.
*/
@SuppressWarnings( { "unchecked" } )
public static boolean inOrder( Comparable[] array )
{
for ( int i = 1; i < array.length; i++ )
{
if ( array[ i ].compareTo( array[ i - 1 ] ) < 0 )
{
return false;
}
}
return true;
}
/**
* Is this List already in order according to its Comparable
interface?
*
* @param list List of Comparable objects.
*
* @return true if array already in order.
*/
public static <T extends Comparable<? super T>> boolean inOrder(
List<T> list )
{
final int length = list.size();
for ( int i = 1; i < length; i++ )
{
if ( list.get( i ).compareTo( list.get( i - 1 ) ) < 0 )
{
return false;
}
}
return true;
}
/**
* Is this array already in order according to a given Comparator?
*
* @param array array of Objects.
* @param comparator Comparator for objects in the array.
*
* @return true if array already in order.
*/
public static <T> boolean inOrder( T[] array, Comparator<? super
T> comparator )
{
for ( int i = 1; i < array.length; i++ )
{
if ( comparator.compare( array[ i ], array[ i - 1 ] ) < 0
)
{
return false;
}
}
return true;
}
/**
* Is this List already in order according to a given Comparator?
*
* @param list List of objects.
* @param comparator Comparator for objects in the list.
*
* @return true if array already in order.
*/
public static <T> boolean inOrder( List<T> list, Comparator<?
super T> comparator )
{
final int length = list.size();
for ( int i = 1; i < length; i++ )
{
if ( comparator.compare( list.get( i ), list.get( i - 1 )
) < 0 )
{
return false;
}
}
return true;
}
}
--
Roedy Green Canadian Mind Products
http://mindprod.com
For me, the appeal of computer programming is that
even though I am quite a klutz,
I can still produce something, in a sense
perfect, because the computer gives me as many
chances as I please to get it right.