Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10464

Re: Verifying a list is alphabetized

From Roedy Green <see_website@mindprod.com.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Verifying a list is alphabetized
Date 2011-12-03 03:10 -0800
Organization Canadian Mind Products
Message-ID <qr0kd7hppg3r0ja5pqd7gqapfi7u6o0srk@4ax.com> (permalink)
References <722147ec-ab43-4d7c-8b41-32f8705ee7db@i8g2000vbh.googlegroups.com> <207hd7h05rmodleca7qv41h73luek13qmm@4ax.com>

Show all headers | View raw


On Fri, 02 Dec 2011 01:43:13 -0800, Roedy Green
<see_website@mindprod.com.invalid> 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?

improved version:

/*
 * [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
 *  1.1 2011-12-03 improve efficiency of List version for Linkedlist
or other List that does not index quickly.
 *                 The problem was pointed out by Patricia Shanahan.
 */
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.1 2011-12-03 improve efficiency of List version for
Linkedlist or other List that does not index quickly.
 *                         The problem was pointed out by Patricia
Shanahan.
 * @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, e.g. ArrayList or
LinkedList
     *
     * @return true if array already in order.
     */
    public static <T extends Comparable<? super T>> boolean inOrder(
List<T> list )
        {
        T prev = null;
        for ( T item : list )
            {
            if ( prev != null && item.compareTo( prev ) < 0 )
                {
                return false;
                }
            prev = item;
            }
        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. , e.g. ArrayList, LinkedList
     * @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 )
        {
        T prev = null;
        for ( T item : list )
            {
            if ( prev != null && comparator.compare( item, prev ) < 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.
 

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Verifying a list is alphabetized laredotornado <laredotornado@zipmail.com> - 2011-11-30 07:52 -0800
  Re: Verifying a list is alphabetized Knute Johnson <nospam@knutejohnson.com> - 2011-11-30 08:05 -0800
    Re: Verifying a list is alphabetized Lew <lewbloch@gmail.com> - 2011-11-30 11:18 -0800
    Re: Verifying a list is alphabetized Arne Vajhøj <arne@vajhoej.dk> - 2011-12-02 19:54 -0500
      Re: Verifying a list is alphabetized Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-03 07:44 -0500
        Re: Verifying a list is alphabetized Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-03 09:02 -0500
        Re: Verifying a list is alphabetized Roedy Green <see_website@mindprod.com.invalid> - 2011-12-03 20:05 -0800
          Re: Verifying a list is alphabetized Roedy Green <see_website@mindprod.com.invalid> - 2011-12-03 22:13 -0800
            Re: Verifying a list is alphabetized Tom Anderson <twic@urchin.earth.li> - 2011-12-04 21:47 +0000
          Re: Verifying a list is alphabetized Martin Gregorie <martin@address-in-sig.invalid> - 2011-12-04 12:42 +0000
            Re: Verifying a list is alphabetized Patricia Shanahan <pats@acm.org> - 2011-12-04 12:18 -0800
              Re: Verifying a list is alphabetized Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-04 13:17 -0800
                Re: Verifying a list is alphabetized Patricia Shanahan <pats@acm.org> - 2011-12-04 15:32 -0800
                Re: Verifying a list is alphabetized markspace <-@.> - 2011-12-04 15:59 -0800
                Re: Verifying a list is alphabetized Patricia Shanahan <pats@acm.org> - 2011-12-04 16:14 -0800
                Re: Verifying a list is alphabetized markspace <-@.> - 2011-12-04 17:12 -0800
                Re: Verifying a list is alphabetized Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-04 21:17 -0500
            Re: Verifying a list is alphabetized Tom Anderson <twic@urchin.earth.li> - 2011-12-04 22:11 +0000
              Re: Verifying a list is alphabetized Martin Gregorie <martin@address-in-sig.invalid> - 2011-12-04 23:41 +0000
              Re: Verifying a list is alphabetized Roedy Green <see_website@mindprod.com.invalid> - 2011-12-12 01:39 -0800
          Re: Verifying a list is alphabetized Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-04 08:47 -0500
          Re: Verifying a list is alphabetized Gene Wirchenko <genew@ocis.net> - 2011-12-04 21:34 -0800
  Re: Verifying a list is alphabetized Patricia Shanahan <pats@acm.org> - 2011-11-30 12:52 -0800
    Re: Verifying a list is alphabetized laredotornado <laredotornado@zipmail.com> - 2011-12-01 06:43 -0800
  Re: Verifying a list is alphabetized Joshua Maurice <joshuamaurice@gmail.com> - 2011-11-30 13:16 -0800
  Re: Verifying a list is alphabetized Roedy Green <see_website@mindprod.com.invalid> - 2011-12-02 01:43 -0800
    Re: Verifying a list is alphabetized Patricia Shanahan <pats@acm.org> - 2011-12-02 05:58 -0800
      Re: Verifying a list is alphabetized Arne Vajhøj <arne@vajhoej.dk> - 2011-12-02 19:55 -0500
      Re: Verifying a list is alphabetized Roedy Green <see_website@mindprod.com.invalid> - 2011-12-03 02:54 -0800
        Re: Verifying a list is alphabetized Patricia Shanahan <pats@acm.org> - 2011-12-03 06:17 -0800
    Re: Verifying a list is alphabetized Roedy Green <see_website@mindprod.com.invalid> - 2011-12-03 03:10 -0800
  Re: Verifying a list is alphabetized Arne Vajhøj <arne@vajhoej.dk> - 2011-12-02 19:52 -0500

csiph-web