Code as follows
public static void main(String[] args) {
ArrayList<String> arrayList=new ArrayList<String>();
arrayList.add("1001");
arrayList.add("999");
String val="";
boolean unsorted=true;
if (val.contains("isAscending")) {
for (int i = 0; i < arrayList.size() - 1; i++) {
if (arrayList.get(i).toLowerCase().compareTo(arrayList.get(i + 1).toLowerCase()) <= 0) {
unsorted = false;
System.out.println(unsorted);
} else {
break;
}
}
} else {
for (int i = 0; i < arrayList.size() - 1; i++) {
if (arrayList.get(i).toLowerCase().compareTo(arrayList.get(i + 1).toLowerCase()) >= 0) {
unsorted = false;
System.out.println(unsorted+" "+"descending");
} else {
break;
}
}
}
}
the above program works correctly but the problem is when I replace the arraylist with the below arraylist it doesn't work
arrayList.add("22");
arrayList.add("8");
I meant to say that if the first index is followed by a 1 digit number it fails
the same repeats here
arrayList.add("1001");
arrayList.add("1000");
for this the above code works
arrayList.add("1001");
arrayList.add("999");
for this it fails
The requirement is to verify if an arraylist is in sorted order or not no need to sort the arraylist