Reputation: 18572
I need to estimate if the array list is sorted (don't sort).
When Strings are sorted, they are in alphabetical order. I try to use compareTo() method to determine which string comes first
And return true if the array list is sorted, else false.
Code:
public boolean isSorted()
{
boolean sorted = true;
for (int i = 1; i < list.size(); i++) {
if (list.get(i-1).compareTo(list.get(i)) != 1) sorted = false;
}
return sorted;
}
Easy test:
ArrayList<String> animals = new ArrayList<String>();
ArrayListMethods zoo = new ArrayListMethods(animals);
animals.add("ape");
animals.add("dog");
animals.add("zebra");
//test isSorted
System.out.println(zoo.isSorted());
System.out.println("Expected: true");
animals.add("cat");
System.out.println(zoo.isSorted());
System.out.println("Expected: false");
animals.remove("cat");
animals.add(0,"cat");
System.out.println(zoo.isSorted());
System.out.println("Expected: false");
**Output:**
false
Expected: true
false
Expected: false
false
Expected: false
This easy test shows only 1/3
coverage.
How to solve this issue.
Upvotes: 5
Views: 22576
Reputation: 3602
You can write a utily method like isSortedList(List list)
.
public static boolean isSortedList(List<? extends Comparable> list)
{
if(list == null || list.isEmpty())
return false;
if(list.size() == 1)
return true;
for(int i=1; i<list.size();i++)
{
if(list.get(i).compareTo(list.get(i-1)) < 0 )
return false;
}
return true;
}
As as utility method, you can use it anywhere.
Upvotes: 1
Reputation: 35557
Try this
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Sort {
public static void main(String []args) {
List<String> l1=new ArrayList<String>();
List<String> l2=new ArrayList<String>();
l1.add("a");
l1.add("b");
l1.add("c");
l2.add("b");
l2.add("c");
l2.add("a");
if(isSorted(l1)){
System.out.println("already sorted");
}
else{
Collections.sort(l1);
}
}
public static boolean isSorted(List<String> list){
String previous = "";
for (String current: list) {
if (current.compareTo(previous) < 0)
return false;
previous = current;
}
return true;
}
}
Upvotes: 1
Reputation: 9237
You need to check for unsorted case.
This means that if you are assuming sorting ascending, the unsorted case will be finding an element at index i
being out of order from index i-1
where element[i] < element[i-1]
.
Upvotes: 0
Reputation: 65
you have to change the compareTo expresion to any positive number, which indicates that the previous element is alphabetically after the current element, hence the list is not ordered
public boolean isSorted()
{
boolean sorted = true;
for (int i = 1; i < list.size(); i++) {
if (list.get(i-1).compareTo(list.get(i)) > 0) sorted = false;
}
return sorted;
}
Upvotes: 0
Reputation: 49372
Change the condition :
if (list.get(i - 1).compareTo(list.get(i)) >0)
You should check for >0
instead of !=-1
.
Go through the documentation of compareTo()
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.
Upvotes: 2
Reputation: 7986
You have a small bug in your method. Should be :
public boolean isSorted()
{
boolean sorted = true;
for (int i = 1; i < list.size(); i++) {
if (list.get(i-1).compareTo(list.get(i)) > 0) sorted = false;
}
return sorted;
}
>0
instead of !=1
, you can't be sure that 1
is returned..
Upvotes: 8