设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);
	            }
	        });

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-21
  • 2021-11-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
  • 2022-01-11
  • 2022-12-23
  • 2021-09-07
  • 2022-12-23
相关资源
相似解决方案