0

Can anybody tell what is the best(easy) way to sort the following string 'index' array in java

String [] menuIndex= { "0",
                       "1",
                       "2",
                       "3",
                       "0.0",
                       "0.0.1",
                       "0.0.0",
                       "0.0.4",
                       "14" ,
                       "14.0",
                       "0.1"  };
I need the sorted array in the following format
0, 
0.0, 
0.0.0,
0.0.1,
0.0.4,
0.1,
1,
2,
3,
14,
14.0

Plz help....

3
  • 1
    Those aren't valid string literals. I assume you mean { "0", "1", etc... }? Commented Feb 7, 2012 at 11:42
  • 1
    Can these number be > 10, and in that case are the lower ones zero-padded? Commented Feb 7, 2012 at 11:48
  • Plz check.. I modified the question Commented Feb 7, 2012 at 11:54

3 Answers 3

3

Since you have changed requirements, your own comparator is the right solution.

import java.util.Arrays;
import java.util.Comparator;

public class MyCmp implements Comparator<String> {

    @Override
    public int compare(String o1, String o2) {
        String[] parts1 = o1.split("\\.");
        String[] parts2 = o2.split("\\.");
        int max = Math.max(parts1.length, parts2.length);
        for (int i = 0; i < max; i++) {
            if (i < parts1.length && i < parts2.length) {
                Integer i1 = Integer.parseInt(parts1[i]);
                Integer i2 = Integer.parseInt(parts2[i]);
                if (i1 == i2)
                    continue;
                return i1.compareTo(i2);
            }
            if (i < parts1.length) {
                return 1;
            }
            if (i < parts2.length) {
                return -1;
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        String[] menuIndex = { "0",
                "1",
                "2",
                "3",
                "0.0",
                "0.0.1",
                "0.0.0",
                "0.0.4",
                "14",
                "14.0",
                "0.1" };
        Arrays.sort(menuIndex, new MyCmp());
        System.out.println(Arrays.toString(menuIndex));
    }

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

1 Comment

Plz check.. I modified the question
1

Create your own comparator and sort the array using it.

1 Comment

It is too complex, for him it is enough standard solution with Arrays.sort()
0

Use Arrays.sort method for sorting..below is the code.

  String [] menuIndex= { "0","1","2","3","0.0","0.0.1","0.0.0","0.0.4","4","4.0","0.1"};
            Arrays.sort(menuIndex);
            for(String str:menuIndex){
                System.out.println(str);
            }

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.