java8 数组排序:
目前我只会先变为list 在转数组不会一次性转数组(还得继续学习) 反序: Integer[]str={1,3,4,5,7,2}; List<Integer> collect = Arrays.stream(str).sorted(Comparator.reverseOrder()).collect(Collectors.toList()); Integer[] integers = collect.toArray(new Integer[6]); String s = Arrays.toString(integers); System.out.println(s); 输出:[7, 5, 4, 3, 2, 1] 正序: Comparator.reverseOrder() 改为 Comparator.naturalOrder() 就行如果是一个对象的一个属性排序则:
List<FollowIMSI> collect = listResult.stream() .sorted(Comparator.comparing(FollowIMSI::getFollowDegree).reversed()) .collect(Collectors.toList());如果两个对象的一个属性排序:
List<Human> humans = new ArrayList<>(); humans.add(new Human("Sarah", 10)); humans.add(new Human("Jack", 12)); 正序是: Collections.sort(humans, Comparator.comparing(Human::getName)); 反序是: Collections.sort(humans, Comparator.comparing(Human::getName).reversed());多条件排序:
//先按年龄从小到大排序,年龄相同再按id从小到大排序 personList.stream().sorted(Comparator.comparing((Person::getAge).thenComparing(Person::getId())).collect(Collectors.toList())