1

012@TEST1 524@TEST2 ABC@TEST3 AB@TEST4 53@TEST5 @TEST6

i want to sort the following data such that Final output: Sorted on the basis of data before ‘@’ Numbers should come before alpha

@TEST6 012@TEST1 53@TEST5 524@TEST2 AB@TEST4 ABC@TEST3

i want to implement this in java ...help me out

2 Answers 2

3
List<String> stringList = Arrays.asList(new String[]{"012@TEST1", "524@TEST2","ABC@TEST3" ,"AB@TEST4" ,"53@TEST5","@TEST6"});

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

        public int compare(String s1,String s2){  
            String c1,c2;
            if (s1.split("@").length >1){c1 = s1.split("@")[0]}else{c1 = ""}
            if (s2.split("@").length >1){c2 = s2.split("@")[0]}else{c2 = ""}
            return c1.compare(c2);
        }
});

Or something like that, just tweak the compare method

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

3 Comments

i am not aable to understand the code please can anyone give some other way out or help me understand the code ..thanks
Are you looking for a pseudo-code algorithm or an implementation in a specific language (possibly using libraries provided by that platform)? This example looks like Java using a sort routine from the platform libraries.
My example is written in Java isn't what you needed?
0

its a regular alpha-numeral dictionary sort, but @ first gets priority, because sadly in the ASCII table, @ got stack between and numbers and the big letters. as you can see here:

http://www.asciitable.com/index/asciifull.gif

alternately, you can store the data in double-valued nodes, that contains the data from the left of the @ in node1, and the data from the right, in node2, and use dictionary sort on node1

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.