How to use a Java8 lambda to sort a stream in reverse order?

后端 未结 12 1673
暗喜
暗喜 2020-11-29 17:25

I\'m using java lambda to sort a list.

how can I sort it in a reverse way?

I saw this post, but I want to use java 8 lambda.

Here is my code (I used

相关标签:
12条回答
  • 2020-11-29 17:59

    If your stream elements implements Comparable then the solution becomes simpler:

     ...stream()
     .sorted(Comparator.reverseOrder())
    
    0 讨论(0)
  • 2020-11-29 18:01

    Instead of all these complications, this simple step should do the trick for reverse sorting using Lambda .sorted(Comparator.reverseOrder())

    Arrays.asList(files).stream()
    .filter(file -> isNameLikeBaseLine(file, baseLineFile.getName()))
    .sorted(Comparator.reverseOrder()).skip(numOfNewestToLeave)
    .forEach(item -> item.delete());
    
    0 讨论(0)
  • 2020-11-29 18:03

    Sort file list with java 8 Collections

    Example how to use Collections and Comparator Java 8 to sort a File list.

    import java.io.File;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class ShortFile {
    
        public static void main(String[] args) {
            List<File> fileList = new ArrayList<>();
            fileList.add(new File("infoSE-201904270100.txt"));
            fileList.add(new File("infoSE-201904280301.txt"));
            fileList.add(new File("infoSE-201904280101.txt"));
            fileList.add(new File("infoSE-201904270101.txt"));
    
            fileList.forEach(x -> System.out.println(x.getName()));
            Collections.sort(fileList, Comparator.comparing(File::getName).reversed());
            System.out.println("===========================================");
            fileList.forEach(x -> System.out.println(x.getName()));
        }
    }
    
    0 讨论(0)
  • 2020-11-29 18:04

    This can easily be done using Java 8 and the use of a reversed Comparator.

    I have created a list of files from a directory, which I display unsorted, sorted and reverse sorted using a simple Comparator for the sort and then calling reversed() on it to get the reversed version of that Comparator.

    See code below:

    package test;
    
    import java.io.File;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.Date;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class SortTest {
        public static void main(String... args) {
            File directory = new File("C:/Media");
            File[] files = directory.listFiles();
            List<File> filesList = Arrays.asList(files);
    
            Comparator<File> comparator = Comparator.comparingLong(File::lastModified);
            Comparator<File> reverseComparator = comparator.reversed();
    
            List<File> forwardOrder = filesList.stream().sorted(comparator).collect(Collectors.toList());
            List<File> reverseOrder = filesList.stream().sorted(reverseComparator).collect(Collectors.toList());
    
            System.out.println("*** Unsorted ***");
            filesList.forEach(SortTest::processFile);
    
            System.out.println("*** Sort ***");
            forwardOrder.forEach(SortTest::processFile);
    
            System.out.println("*** Reverse Sort ***");
            reverseOrder.forEach(SortTest::processFile);
        }
    
        private static void processFile(File file) {
            try {
                if (file.isFile()) {
                    System.out.println(file.getCanonicalPath() + " - " + new Date(file.lastModified()));
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 18:09

    Alternative way sharing:

    ASC

    List<Animal> animals = this.service.findAll();
    animals = animals.stream().sorted(Comparator.comparing(Animal::getName)).collect(Collectors.toList());
    

    DESC

    List<Animal> animals = this.service.findAll();
    animals = animals.stream().sorted(Comparator.comparing(Animal::getName).reversed()).collect(Collectors.toList());
    
    0 讨论(0)
  • 2020-11-29 18:11

    You can adapt the solution you linked in How to sort ArrayList<Long> in Java in decreasing order? by wrapping it in a lambda:

    .sorted((f1, f2) -> Long.compare(f2.lastModified(), f1.lastModified())

    note that f2 is the first argument of Long.compare, not the second, so the result will be reversed.

    0 讨论(0)
提交回复
热议问题