java 中以逗号分隔的纯数字字符串从小到大排序

**

将以纯数字以逗号分隔的字符串进行从小到大排序:

**

例如:
str = “1,23,2,3,45,56,18,34,78”;
结果:1,2,3,18,23,34,45,56,78

package util;


import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
//将字符串从大到小按顺序排序:
public class Test {
    public static void main(String[]args){
        try {
            String str = "1,23,2,3,45,56,18,34,78";
            HashSet set = new HashSet();//set 去重
            for (int i = 0; i < str.split(",").length; i++) {
                set.add(Integer.parseInt(str.split(",")[i]));
            }
            List list = new ArrayList();
            list.addAll(set);
            Collections.sort(list);//排序
            System.out.println(list);       //[1, 2, 3, 18, 23, 34, 45, 56, 78]
            String liset1 = ""+list;
            System.out.println(liset1);            //[1, 2, 3, 18, 23, 34, 45, 56, 78]
            String list3 = StringUtils.join(list,",");
            System.out.println("liset3:"+list3);            //1,2,3,18,23,34,45,56,78
            boolean list4 = list3.equals("1,2,3,18,23,34,45,56,78");
            System.out.println("list4:"+list4);         //true

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Java)