自定义排序,字符串排序,Comparator

//既定顺序
List sortStrings = Arrays.asList("香蕉", "苹果", "梨子", "芒果", "橙子");
//需要排序
List needToSort = Arrays.asList("苹果", "香蕉", "苹果", "橙子", "芒果","梨子");
//通过对比 需要比较元素在ArrayList的index 就可以得到比较方法
//比for循环简洁
List stringList=needToSort.stream().sorted(Comparator.comparingInt(sortStrings::indexOf)).collect(Collectors.toList());
//输出
sortStrings.forEach(System.out::print);
System.out.println("");
needToSort.forEach(System.out::print);
System.out.println("");
stringList.forEach(System.out::print);

原文地址:https://www.cnblogs.com/onexixi/p/12398717.html