2
votes

I have searched a lot for two days and i was not successful ,

Now i have strings with 7 integer numbers ( both + and - ) separated with comma .

I have written a sample code to explain .

        ArrayList<String> str = new ArrayList<String>();

        str.add("9,-9,21,23,28,29,35");
        str.add("18,18,-21,28,28,32,34");
        str.add("-11,-11,22,28,29,-30,31");
        str.add("8,-8,26,31,31,31,31");
        str.add("8,8,26,-32,25,29,35");
        str.add("10,9,-21,45,25,29,35");
        str.add("-11,59,21,25,25,-29,35");
        str.add("12,-9,21,55,25,29,15");
        str.add("9,9,21,25,25,-29,35");
        str.add("7,9,21,25,-35,25,35");
        str.add("4,-39,21,-15,25,-29,35");
        str.add("9,9,21,25,27,29,-35");
        str.add("10,9,21,35,25,39,15");
        str.add("8,-9,21,-25,25,29,-35");
        str.add("18,-9,21,-23,25,29,-35");

        Collections.sort(str);

this does not return the correct sorted array . It tests with the first digit of the numbers and proceed with the sorting .

But what i want is , the sorting must be based on the first numbers in the string . Only if the numbers are same ( say there are three 9 in the first numbers of string arrays ), it should check for the second numbers in those(tied strings alone) and sort accordingly and so on .

the result should be as

9 , -9 , 21 , 23 , 28 , 29 , 35
9 , 9 , 21 , 25 , 25 , -29 , 35
9 , 9 , 21 , 25 , 27 , 29 , -35

Is there any method to sort in this method . Please let me know if any , any related answers are welcomed .

Thanks in advance .

4
Do you want to sort ArrayList or elements in the ArrayList, please make it clear...Pragnani
i want to sort the array list not the elements in the list .VIGNESH

4 Answers

1
votes

Use this comparing method as the comparator for sort(List list, Comparator c):

Collections.sort(str, new Comparator<String>(){                    
    public int compare(String str1, String str2){
      int result = 0;
      int i = 0;
      String [] s1 = str1.split(",");
      String [] s2 = str2.split(",");
      while ((i < s1.length) && (i < s2.length) && (result == 0)){
        result = (Integer.parseInt(s1[i]) - Integer.parseInt(s2[i]));
        i++;        
      }
      return (result);
    }
});
2
votes

You are using the incorrect data type for the sorting semantics you want. Java sees you want to sort strings, so it sorts them - by lexicographical ordering, because you haven't told it to do otherwise. Java is no mind reader :)

Instead of attempting to sort strings, as in "9,-9,21,23,28,29,35", sort arrays of integers, as in {9, 9, -9, 21, 23, 28, 29, 35}. You will still have to write your own logic for the comparator, but it will be comparatively easy now as you will not have to do any string parsing.

If the data you need to sort arrives at your program in string format, try split on ',' then parse each component of the string array to an int, and finally dump it all into an int array or ArrayList.

1
votes

Write your custom sorting logic and pass through Collection#sort.

Collections.sort(str, new Comparator<String>(){
                          public int compare(String str1, String str2){
                               // Write your logic
                               // @return a negative integer, zero, or a  
                              // positive integer as the first argument is less 
                              //  than, equal to, or greater than the second.
                          }
                      });
0
votes

You can create a new class say NumberListString implementing Comparable interface with this string as a private field. Provide appropriate constructors as well as accessors/mutators.

Now override the compareTo method and provide your logic for this comparison.

Then for sorting use Collections.sort(str). This will sort your list in the required manner.

Alternatively you can create an anonymous comparator on the fly and supply it to your Collections.sort method.

Note: I will recommend the first way as it allows you to abstract other operations that you might need to perform on this particular type of string.