简体   繁体   中英

sorting an arraylist of string with numerical values in java

I need to sort an arraylist of string which contains numerical values. The arraylist is as follows

A\B=5\C=56,
A\B=2\C=1,
A\B=2\C=25,
D\E=1,
D\E=3\F=5,
D\E=3\F=4 
etc... 

Sorting order I require is :

A\B=2\C=1
A\B=2\C=25
A\B=5\C=56
D\E=1
D\E=3\F=4
D\E=3\F=5

您将要实现Comparator<String> ,在其中可以确定任意两个字符串的大于/小于行为。

Then, you can sort it via Collections.sort(list, myNewComparator);

List<String> list = new ArrayList<String>();
        list.add("A\\B=5\\C=56");
        list.add("A\\B=2\\C=1");
        list.add("A\\B=2\\C=25");
        list.add("D\\E=1");
        list.add("D\\E=3\\F=5");
        list.add("D\\E=3\\F=4");
        Collections.sort(list);
        System.out.println(list);

If you want to custom your sorting then try for go for the previous answer posted.

使用Collections.sort ,但是我认为您必须编写自己的Comparator。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM