Reputation: 31
I'm checking if the strings in my array are arranged in alphabetical order. My codechecker is saying that my code fails to account for some cases, but I'm really not sure how to change it.
EDIT: Apparently my code returns "true" when checking the array "cat ape dog zebra", which is clearly false.
public boolean isSorted()
{
boolean sorted = true;
for(int i = 0; i < list.size(); i++)
{
for(int j = i+1; j < list.size(); j++)
{
if (list.get(i).compareTo(list.get(j)) == 1)
{
sorted = false;
}
}
}
return sorted;
}
Upvotes: 0
Views: 17297
Reputation: 828
I know it's a Java question, but here's how I ended up implementing this in Kotlin very succinctly:
myList.zipWithNext { a, b ->
if (a > b) {
fail("Expected a sorted list, but $a > $b")
}
}
Upvotes: 0
Reputation: 3735
I guess you're using a instance variable to save a list of String
. Try this code where I use Collections.sort()
:
public boolean isSorted() {
// Copies all of the elements from one list into another.
List<String> listSorted = new ArrayList<String>(list);
// Sorts the new list.
Collections.sort(listSorted);
// Check if both of list are equals.
return listSorted.equals(list);
}
Upvotes: 4
Reputation: 17971
Using String.compareTo()
is a very easy way to do it.
String.compareTo() returns a negative number if the string precedes the argument of the method, 0 if it is the same, and a positive number if the string comes after the argument of the method
The way you did it:
if (list.get(i).compareTo(list.get(j)) == 1)
Is very close, but it should be
if (list.get(i).compareTo(list.get(j)) > 0)
You could use that alongside of a comparator to quickly sort, or in your case check to see if it is sorted
boolean isSorted(String[] words) {
for (int i = 0; i < words.length()-1; i++) {
if (words[i].compareTo(words[i+1] >= 0) {
return false;
}
}
return true;
}
Or if you wanted to sort them, this would work:
Collections.sort(fooList,
new Comparator<String>()
{
public int compare(String s1, String s2)
{
return s1.compareTo(s2);
}
});
Or to return true or false
Upvotes: 0
Reputation: 30723
compareTo() returns a positive value (not necessarily 1) if the string on the left-hand-side is "greater" than the one on the right. Thus, you need to change the condition from == 1
to >= 1
.
Also, you don't need a second loop (j) that runs over all elements. You just need to compare two consecutive elements, as follows:
public boolean isSorted() {
for(int i = 1; i < list.size(); i++)
if (list.get(i).compareTo(list.get(i - 1)) >= 1)
return false;
return true;
}
Upvotes: 0
Reputation: 1451
It's far easier than it looks: just iterate over the list, checking if adjacent elements are in the right order. If all adjacent pairs are in order, then the whole list is.
public boolean isSorted()
{
for(int a=0;a<list.size()-1;a++)
{
if(list.get(a).compareTo(list.get(a+1))>0)
{
return false;
}
}
return true;
}
Upvotes: 2
Reputation: 22064
if (list.get(i).compareTo(list.get(j)) == 1)
The above line is erroneous. The returned value would be positive and not strictly equal to 1.
Try changing to
if (list.get(i).compareTo(list.get(j)) >0)
Upvotes: 7