1

Hello everyone I have a code using an arraylist, these are the test inputs to be added. But after I use the sort method. The output is not the one I expected.

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

test.add("2,2,17");    
test.add("5,4,24 ");  
test.add("8,1,11");    
test.add("19,0,0");  
test.add("2,3,21");  
test.sort(null);  

Output :
19,0,0
2,2,17
2,3,21
5,4,24
8,1,11

My desired out put should be :
2,2,17
2,3,21
5,4,24
8,1,11
19,0,0

Is there a way to sort "19,0,0" to be at the end, or any number to be add to make it the end of the arrayList?

8
  • 1
    how are you sorting? Commented Oct 14, 2016 at 14:21
  • 1
    When comparing String's 1 comes before 2 so 19 will come before 2...Sounds like you want to compare Integer's. Commented Oct 14, 2016 at 14:22
  • Can you explain your data's semantics? Commented Oct 14, 2016 at 14:23
  • 1
    What Itamar is saying, we don't know how you compare anything. I'm taking a wild stab in the dark, but to me it just seems like you're grabbing the first element in the array and comparing it after parsing the String into an Integer and thus you have the data you are showing now. If you grabbed the first element, then you would end up getting "1" at some point, and because it is "less than" all the other first elements {2, 2, 5, 8 } it will move up first. Again this is just a stab in the dark, and maybe I'm wrong here. I'd say don't compare just the first element, and use a delimiter (,) Commented Oct 14, 2016 at 14:25
  • @ItamarGreen, yes it's comparing integer but in string format. But I want to work string arraylist. i hoping if I can add a magical number for each values at the end and after sorting ("19,0,0 + magical number") will be at the end. Commented Oct 14, 2016 at 14:28

4 Answers 4

1

You'll want to use Collections.sort, something like this:

Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                int s1int = Integer.parseInt(s1.substring(0, s1.indexOf(",")));
                int s2int = Integer.parseInt(s2.substring(0, s2.indexOf(",")));
                return s1int - s2int;
            }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Sir thank you very much it worked, although Ill try to understand this kind of process.
Great - just keep in mind the code makes some assumptions that you'll want to safeguard. So for example if there's a list entry without a comma, it will probably crash/be unable to parse.
0

You can use Collections.sort() https://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator). And define a Comparator to compare the objects in the list.

Comments

0

In order for the strings to be sorted alphabetically rather than numerically, you will need to implement a comparator that converts the strings to integers for the comparison. You can then use this comparator with Collections.sort() to sort your list.

A better option would be to store your integers as integers in a 2D array rather than as strings (or some kind of nested list if the dimensions are not known up-front); however, I'd need to know more about how the data is created and used before uniformly proclaiming this to e the solution.

Comments

0

A possible flexible solution for Strings of various 'lengths', i.e. a different number of integers separated by commas.

Example list

2,2,17
5,4,24
19,0,2
8,1,11
19,0,1,2
19,0,1,4
2,3,21
2

Result after sorting

2
2,2,17
2,3,21
5,4,24
8,1,11
19,0,1,2
19,0,1,4
19,0,2

Code

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortExample {

    public static void main(String[] args) {
        List<String> test = new ArrayList<>();
        test.add("2,2,17");
        test.add("5,4,24");
        test.add("19,0,2");
        test.add("8,1,11");
        test.add("19,0,1,2");
        test.add("19,0,1,4");
        test.add("2,3,21");
        test.add("2");

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

            @Override
            public int compare(String s1, String s2) {
                String[] split1 = s1.split(",");
                String[] split2 = s2.split(",");
                final int indicesToCheck = Math.min(split1.length, split2.length);
                int result = 0;
                int i = 0;
                while (result == 0 && i < indicesToCheck) {
                    result = Integer.compare(Integer.parseInt(split1[i]),
                                             Integer.parseInt(split2[i]));
                    i++;
                }
                return result == 0 ? Integer.compare(split1.length, split2.length) : result;
            }
        });

        for (String s : test) {
            System.out.println(s);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.