mino
mino

Reputation: 7288

Java - Checking if an ArrayList of String are in alphabetical order

I have an ArrayList called account which contains Strings. I'm trying to write a method that checks if they are in order and returns true or false based on whether they are in order or not.

How would you go about this? I've already tried checking the initial chracter with a for-loop but it went terribly wrong. I created a new ArrayList and set it equal to the original, then sorted it and compared them but since they contained the same data it always came back true.

Just an extra quick question, since I'm doing this for Strings, how would you check if some numbers were in ascending/descending order? Throught the same principal?

Thankyou!

Upvotes: 7

Views: 26008

Answers (6)

Piotr Findeisen
Piotr Findeisen

Reputation: 20730

If you don't mind using external library (Guava) Ordering will do:

boolean isSorted = Ordering.natural().isOrdered(list);

This will do for String and other Comparables. If you are check ordering of some custom type, use any of the static factory methods in the Ordering class or subclass it.

Edit for case-insensitive ordering use:

boolean isSorted = Ordering.from(String.CASE_INSENSITIVE_ORDER).isOrdered(list);

Upvotes: 6

Tudor
Tudor

Reputation: 62439

Just use a loop and check if they are in order:

boolean isSorted = true;
for(int i = 0; i < list.size() - 1; i++) {
   // current String is > than the next one (if there are equal list is still sorted)
   if(list.get(i).compareToIgnoreCase(list.get(i + 1)) > 0) { 
       isSorted = false;
       break;
   }
}

Upvotes: 0

Finbarr
Finbarr

Reputation: 32126

I think a for loop would be suitable for this. The approach I would take would be to check each word against the previous and see if they are in the correct alphabetical ordering. Best case this is O(2) for determining that the list is out of order, worst case O(n) for telling you that the list is in order.

Edit: fge's answer above outlines the code for approach described.

Upvotes: 2

fge
fge

Reputation: 121720

Try this (assuming you want to compare the strings using their natural ordering, of course):

String previous = ""; // empty string: guaranteed to be less than or equal to any other

for (final String current: thelist) {
    if (current.compareTo(previous) < 0)
        return false;
    previous = current;
}

return true;

This is due to the fact that String implements Comparable<String>, and the comparison will be done using the strings' natural ordering.

Upvotes: 14

aleroot
aleroot

Reputation: 72636

Use the sort method of Collection class :

List<String> list = new ArrayList<String>();
//Add Elements
Collections.sort(list);

Sorts the specified list into ascending order, according to the natural ordering of its elements.

Upvotes: 1

mishadoff
mishadoff

Reputation: 10789

ArrayList<String> initial = // smth
ArrayList<String> copy = // copy initial list here
Collections.sort(initial);
return initial.equals(copy);

Upvotes: 0

Related Questions