1
   ArrayList<String> listOfSums = new ArrayList<String>();
   listOfSums.add("a = 100");
   listOfSums.add("a + b + c = 100");
   listOfSums.add("a + b = 100");
   listOfSums.add("b + c = 100");
   Collections.sort(listOfSums);

When I sort the array list this way the output is

a + b + c = 100
a + b = 100
a = 100
b + c = 100;

But I want the output to be something like this

a = 100
a + b = 100
a + b + c= 100
b + c = 100

How can I do this?

Birat Bade Shrestha
  • 800
  • 1
  • 8
  • 28
  • 1
    It is difficoult to understand your ordering logic. Pls explain it in your question. Anycase, since you are using not an alphanumerical ordering, you need to implement your own Comparator – Giuseppe Marra Feb 18 '16 at 11:19

1 Answers1

3

Try this

Collections.sort(listofcountries, new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                // TODO Auto-generated method stub
                return o1.replaceAll("[^a-zA-z]", "").compareTo(o2.replaceAll("[^a-zA-z]", ""));
            }
        });
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • Can you explain how that worked? – Birat Bade Shrestha Feb 18 '16 at 11:26
  • 1
    @BiratBadeShrestha You need to understand your pattern of order, It will be like an alphabetical order without any special characters or digits, so you need to replace characters other than alphabets to get the required sorting order – Pragnani Feb 18 '16 at 11:29