List元素是string,里面包含数字,如何排序的问题

设ArrayList<String> list, 里面包含"1aa","2ggg","6aaa","4bbb",如果对里面的数字进行排序。

可以重写Collections.sort方法。

 

	        Collections.sort(list, new Comparator<String>() {
	            public int compare(String o1, String o2) {
	                return extractInt(o1) - extractInt(o2);
	            }

	            int extractInt(String s) {
	                String num = s.replaceAll("\\D", "");
	                // return 0 if no digits found
	                return num.isEmpty() ? 0 : Integer.parseInt(num);
	            }
	        });

  

posted @ 2016-03-03 13:50  啃苹果  阅读(1668)  评论(0编辑  收藏  举报