1

I have a string array. This array is made of three integer array. But each of the Integer is followed by a character. Either l or p or r. now my aim is to sort the string array according to int values. Array sort or Collection.sort does not work properly. In my case if the array is {-2l 1r 0p 4p 3l} the answer should be {-2l 0p 1r 3l 4p}.

  String[] left = new String[starts.length];
            String[] right = new String[ends.length];
            String[] points_fast = new String[points.length];

            for (int i = 0; i < starts.length; i++) {
                left[i] = (starts[i] + "l" + " ");
            }
            for (int i = 0; i < ends.length; i++) {
                right[i] = (ends[i] + "r" + " ");
            }
            for (int i = 0; i < points.length; i++) {
                points_fast[i] = (points[i] + "p" + " ");
            }
            String[] add = new String[left.length + right.length + points_fast.length];
            // add=l+r+p;
            System.arraycopy(left, 0, add, 0, left.length);
            System.arraycopy(right, 0, add, left.length, right.length);
            System.arraycopy(points_fast, 0, add, left.length + right.length, points_fast.length);

my aim is to sort the array "add". N.B I tried to convert it to list and then use collections.sort. It does not work.

1
  • You mean hash map?? Commented Jun 21, 2016 at 22:47

1 Answer 1

2

I hope this satisfy your solution: you just need to use the Collection.sort and override the comparator

 List<String> mnames1 = Arrays.asList("-2l", "1r", "0p", "4p", "3l");
    System.out.println(mnames1+" before");
    Collections.sort(mnames1, new Comparator<String>() {
        @Override
        public int compare(String a, String b) {
            String m = a.substring(0,a.length()-1);
            String n = b.substring(0,b.length()-1);
            return Integer.compare(Integer.parseInt(m),Integer.parseInt(n));
        }
    });
    System.out.println(mnames1+ "after");

output:

[-2l, 1r, 0p, 4p, 3l] before

[-2l, 0p, 1r, 3l, 4p]after

Sign up to request clarification or add additional context in comments.

9 Comments

No. I tried this too. Actually I found this help on some thread. But it does not work.
@Tasmin what wasn't working. just try it and let me know the error message
My input was 1,1,3. So the array was like {1l.1r.3p} The error is Exception in thread "main" java.lang.NumberFormatException: For input string: "1r"
@Tasmin are you making arrays with the dot or what{1.l 1r.3p} this is typo error right. when you use Arrays.asList(1l, 1r, 3p}; , this should work for you because is the same i tried with.
no. in the array there is no dot between the int values and the alphabet l/p/r.
|

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.