- /***
- @author Joney
- * 思路: 1上面Ip按数值大小,排序,有序,用TreeSet存储
- * 2遍历数据,按自定义要求排序 3
- * 用输出流输出到txt中
- */
- public class Test8 {
- public static void main(String[] args) {
- // 定义一个TreeSet数组存入上面ip
- // 创建集合对象
- TreeSet<String> tm = new TreeSet<String>(new Comparator<String>() {
- @Override
- public int compare(String s1, String s2) {
- int num=s1.length()-s2.length();
- int num2=(num==0)?s1.compareTo(s2):num;
- return num2;
- }
- });
-
- // 添加元素
- tm.add("61.54.231.245");
- tm.add("61.54.231.9");
- tm.add("61.54.231.246");
- tm.add("61.54.231.48");
- tm.add("61.53.231.249");
- for(String st:tm){
- System.out.println(st);
- }
- System.out.println("***********************************");
- String []select ={"61.54.231.245","61.54.231.9","61.54.231.246","61.54.231.48","61.53.231.249"};
- Arrays.sort(select);
- for(String e:select){
- System.out.println(e);
- }
- }
- }
复制代码 |