2

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

Philipp
  • 2,787
  • 2
  • 25
  • 27
Varsha
  • 29
  • 2
  • why dont you convert ArrayList to ArrayList?then sort? – Shreyas Sarvothama Oct 26 '16 at 06:02
  • Are all the strings guaranteed to have only digits in them? If so, just use `Integer.parseInt()` before comparing--but then why are you converting to lower case if the strings have only digits? – ajb Oct 26 '16 at 06:06
  • By the way, please don't say "Sort an ArrayList" in the title, and then in your question say "there's no need to sort". It confuses everybody. – ajb Oct 26 '16 at 06:07
  • hi ajb the conversion to lowercase is because what if the arraylist has string values.I just want the method to be used for both if the arraylist contains string or numeric values – Varsha Oct 26 '16 at 06:11
  • What do you mean "has string values"? This is an `ArrayList`, so of course it has string values. The question is, can those string values include letters? If they can, then your question doesn't make sense. – ajb Oct 26 '16 at 06:12
  • @ShreyasSarvothama that's a -7 voted question; is that really a good dupe? – dimo414 Oct 26 '16 at 06:43

2 Answers2

2

If you want to find out if your list be sorted, you can use a custom comparator to sort your ArrayList using the actual integers which are represented by the strings. Then, you can compare this sorted list against the original one.

Collections.sort(arrayList, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        try {
            // try to compare the two values as integers, if possible
            Integer val1 = Integer.valueOf(s1);
            Integer val2 = Integer.valueOf(s2);
            return val1.compareTo(val2);
        }
        catch (NumberFormatException e) {
            // fall back to sort them as strings
            return s1.compareTo(s2);
        }
    }
});

As @ajb mentioned, it might be simpler to just assume that every string can be converted to an integer, and to deal with the exception elsewhere. In this case we can use:

Collections.sort(arrayList, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        return Integer.valueOf(s1).compareTo(Integer.valueOf(s2));
    }
});
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Strictly speaking, this isn't actually a valid comparator, because it breaks transitivity ("2" < "+3", "+3" < "/5", "/5" < "2"). However, I suppose it could be valid if the actual subset of possible strings is limited. – ajb Oct 26 '16 at 06:19
  • @ajb Based on your comments, the OP may not be meaningful at all if there are characters in the list. Anyway, assuming all strings are valid integers my answer should work for him. I added the `try` block just to cover my bases. – Tim Biegeleisen Oct 26 '16 at 06:20
  • If all the strings are supposed to be valid integers, then it might be better to just propagate the exception instead of catching it. Having a comparator that violates the rules can lead to some interesting exceptions being thrown out of Timsort. Might be better to throw an easily understood exception than one of those. But you're right--if the strings are all valid integers, your solution is fine (but for a large list, it might be more efficient to convert everything first and then sort). – ajb Oct 26 '16 at 06:23
0

The method trys to parse the Strings into Integer, if that´s successfull it compares the numbers, if not it compares the Strings.

public class Test {

public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("1");
    arrayList.add("999");
    arrayList.add("1001");

    boolean isAsc = true;
    boolean isSorted = true;

    for (int i = 0; i < arrayList.size() -1; i++) {
            int a, b;
            boolean numbs;
            try {
                int a = Integer.parseInt(arrayList.get(i));
                int b = Integer.parseInt(arrayList.get(i + 1));
                if ((isAsc && a > b) || (!isAsc && a < b)) {
                    isSorted = false;
                    break;
                } 
            } catch (NumberFormatException) {
                 if ((isAsc  && arrayList.get(i).toLowerCase().compareTo(arrayList.get(i + 1).toLowerCase() > 0) || (arrayList.get(i).toLowerCase().compareTo(arrayList.get(i + 1).toLowerCase() < 0)) {
                 isSorted = false;
                 break;
            }
            }
    }

    if (isSorted) {
        System.out.println("List is sorted !");
    } else {
        System.out.println("List is not sorted !");
    }
}
}

Hope I could help

Tim Schmidt
  • 1,297
  • 1
  • 15
  • 30