开发者

Need a algorithm for this sorting problem !

开发者 https://www.devze.com 2023-01-02 12:37 出处:网络
012@TEST1 524@TEST2 ABC@TEST3 AB@TEST4 53@TEST5 @TEST6 i want to sort the following data suc开发者_如何学Ch that

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

i want to sort the following data suc开发者_如何学Ch 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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消