Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 30 Nov 2011 14:52:48 -0600 Date: Wed, 30 Nov 2011 12:52:43 -0800 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Verifying a list is alphabetized References: <722147ec-ab43-4d7c-8b41-32f8705ee7db@i8g2000vbh.googlegroups.com> In-Reply-To: <722147ec-ab43-4d7c-8b41-32f8705ee7db@i8g2000vbh.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 29 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.194.31 X-Trace: sv3-MvNcMDU0B7MKtWmyMYMmQsozKnHeExanjvjOfzD5YBHsd2qk0wJnpm2AuzcH4SFEpLoQUEYSZscmDNQ!aI1+R4zPDbzKXmaJ1jUh4VW4r4rBk6JEdZCTKS8seeMScKOiuysP6qCkR4dCIARN6JJBQmceHGwD!J5VJNPpwu34Tb6maQb5zCR0/tNIhiYMasAvbEQaXIliBTA== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 1965 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10376 On 11/30/2011 7:52 AM, laredotornado wrote: > Hi, > > 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? If you just want to test, scan the list comparing consecutive pairs of elements: public static > boolean isOrdered( List data) { T oldElement = null; for (T currentElement : data) { if (oldElement != null && oldElement.compareTo(currentElement) > 0) { return false; } oldElement = currentElement; } return true; } If you want to achieve sortedness, you are unlikely to save time compared to just doing the sort unconditionally. Patricia