Java – check that the ArrayList of strings is in alphabetical order
I have an ArrayList named account, which contains strings I'm trying to write a method to check whether they are ordered and return true or false according to whether they are ordered
What would you do? I've tried to check the initial character with for loop, but it's very wrong I created a new ArrayList and set it equal to the original, then sorted it and compared it, but because they contain the same data, it always returns to reality
It's just an extra quick question because I'm doing this for strings. How do you check that some numbers are in ascending / descending order? Is it the same principal?
thank you!
Solution
Try this (suppose you want to compare strings using their natural order):
This is because string implements comparable < string >, and will compare using the natural order of strings