[java]代码库
public static void main(String[] args) {
String ip = "211.64.240.23,3.3.3.3,211.64.250.21,18.23.4.57,222.33.43.2";
System.out.println("排序前:" + ip);
// ip地址每个.之间 最少1位最高3位 ,补两位0
ip = ip.replaceAll("(\\d+)", "00$1");
System.out.println("补零后:" + ip);
// 将多余的零去掉,保持.前后的数字 只保留三位
ip = ip.replaceAll("0*(\\d{3})", "$1");
System.out.println("去掉多余的零后:" + ip);
String[] ipArr = ip.split(",");
TreeSet<String> ipSet = new TreeSet<String>();
for (String ip_str : ipArr) {
ipSet.add(ip_str);// 放入TreeSet会自动进行排序
}
System.out.print("排序后:");
for (Object ipObj : ipSet) {
String temp = ((String) ipObj).replaceAll("0*(\\d+)", "$1");
System.out.print(temp + " ");
}
}
//源代码片段来自云代码http://yuncode.net