2

I am studying JAVA and I have a question about Sorting string.

In the String ArrayList We have string value STRUCTURE is "0" + " " + "some string"

for example,

     | String
     | 0 AA
     | 1 BB
     | 2 AA
     | 3 AA
     | 4 CC
     | 5 BB

and

when we sort it, result should be

     | String 
     | 0 AA
     | 2 AA
     | 3 AA
     | 1 BB
     | 5 BB
     | 4 CC

how can I sort the String with "number string" + " " + " String"

thanks

update:

i tested code blow with 1 AA
2 AA
3 BBB
4 CC
5 BBB
6 AA
7 BBB
8 CC
9 ZZZ
10 QQQ

and I got

0 AA

1 AA

5 AA

4 BBB

2 BBB

6 BBB

3 CC

7 CC

9 QQQ

8 ZZZ

1
  • OP, please un accept my answer and accept the answer posted by ARS as its highly efficient then mine, so that i can delete my answer .:) Commented Jan 25, 2013 at 14:43

3 Answers 3

7

Expanding upon the Comparator solution (which is really the way you should go about solving this problem):

Collections.sort(yourList, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        String[] split1 = s1.split(" ");
        String[] split2 = s2.split(" ");

        int n = split1[1].compareTo(split2[1]);

        if (n == 0) {
            return Integer.valueOf(split1[0]).compareTo(
                                  Integer.valueOf(split2[0]));
        }

        return n;
    }
});
Sign up to request clarification or add additional context in comments.

Comments

4

Use Collections.sort(List, Comparator). You'll need to provide your own Comparator implemention, which implements your sorting requirements.

Comments

-4

I dint really consider space and time complexity while coding it. i am not really sureits efficient enough, but it would give you anidea.

  List<String> l = new ArrayList<>();

        l.add("0 CC");
        l.add("1 BB");
        l.add("2 AA");
        l.add("3 AA");

    String str="";
    for(String s: l){
        str+=s+",";
    }
    String[] sArr = str.split(",");
    String temp="";
    for(int i=0; i<sArr.length;i++) {
        for(int j= i+1; j<sArr.length;j++){
            if(sArr[i].split("\\s")[1].compareToIgnoreCase(sArr[j].split("\\s")[1])>0){
                temp= sArr[j];
                sArr[j]= sArr[i];
                sArr[i]=temp;
            }
        }
    }
    for(String g: sArr){
        System.out.println(g);
    }

output:
2 AA
3 AA
1 BB
0 CC

13 Comments

This is a very bad solution. In fact, I'm doubtful that it works for all cases!
@Muel it did work for the given input. can you suggest any other solution ???.. idint really consider space and time complexity while coding it
i tested ur code and I got result, I was mostly fine , but BBB part has order problems. lol but thanks!! (I update my question and code tests result on the above)
@DcRedwing i am sure this can be done more elegantly then my code. but i was only giving you an idea.i am fine with the downvote, it;d kind if they'd also comment :)
The problem with this code is that (if it does work, which I'm not saying it doesn't) it will be really hard to manage/update in the future. It is much better to use a Comparator.
|

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.