List集合String排序

xiaoxiao2021-02-28  151

package com.han.demo; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Demo { public static void main(String[] args) { List<String> strs=new ArrayList<String>(); strs.add("d"); strs.add("z"); strs.add("a"); strs.add("b"); strs.add("h"); strs.add("s"); strs.add("c"); //实现排序方法 Collections.sort(strs, new Comparator() { @Override public int compare(Object o1, Object o2) { String str1=(String) o1; String str2=(String) o2; if (str1.compareToIgnoreCase(str2)<0){ return -1; } return 1; } }); for (int i = 0; i < strs.size(); i++) { System.out.print(strs.get(i)); } } }

转载请注明原文地址: https://www.6miu.com/read-30807.html

最新回复(0)