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

后端 未结 12 1672
暗喜
暗喜 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:48

    If you want to sort by Object's date type property then

    public class Visit implements Serializable, Comparable<Visit>{
    private static final long serialVersionUID = 4976278839883192037L;
    
    private Date dos;
    
    public Date getDos() {
        return dos;
    }
    
    public void setDos(Date dos) {
        this.dos = dos;
    }
    
    @Override
    public int compareTo(Visit visit) {
        return this.getDos().compareTo(visit.getDos());
    }
    

    }

    List<Visit> visits = getResults();//Method making the list
    Collections.sort(visits, Collections.reverseOrder());//Reverser order
    
    0 讨论(0)
  • 2020-11-29 17:54

    You can use a method reference:

    import static java.util.Comparator.*;
    import static java.util.stream.Collectors.*;
    
    Arrays.asList(files).stream()
        .filter(file -> isNameLikeBaseLine(file, baseLineFile.getName()))
        .sorted(comparing(File::lastModified).reversed())
        .skip(numOfNewestToLeave)
        .forEach(item -> item.delete());
    

    In alternative of method reference you can use a lambda expression, so the argument of comparing become:

    .sorted(comparing(file -> file.lastModified()).reversed());
    
    0 讨论(0)
  • 2020-11-29 17:54

    In simple, using Comparator and Collection you can sort like below in reversal order using JAVA 8

    import java.util.Comparator;;
    import java.util.stream.Collectors;
    
    Arrays.asList(files).stream()
        .sorted(Comparator.comparing(File::getLastModified).reversed())
        .collect(Collectors.toList());
    
    0 讨论(0)
  • 2020-11-29 17:55

    Use

    Comparator<File> comparator = Comparator.comparing(File::lastModified); 
    Collections.sort(list, comparator.reversed());
    

    Then

    .forEach(item -> item.delete());
    
    0 讨论(0)
  • 2020-11-29 17:56

    You can define your Comparator with your own logic like this;

    private static final Comparator<UserResource> sortByLastLogin = (c1, c2) -> {
        if (Objects.isNull(c1.getLastLoggedin())) {
            return -1;
        } else if (Objects.isNull(c2.getLastLoggedin())) {
            return 1;
        }
        return c1.getLastLoggedin().compareTo(c2.getLastLoggedin());
    };   
    

    And use it inside foreach as:

    list.stream()
         .sorted(sortCredentialsByLastLogin.reversed())
         .collect(Collectors.toList());
    
    0 讨论(0)
  • 2020-11-29 17:57

    For reverse sorting just change the order of x1, x2 for calling the x1.compareTo(x2) method the result will be reverse to one another

    Default order

    List<String> sortedByName = citiesName.stream().sorted((s1,s2)->s1.compareTo(s2)).collect(Collectors.toList());
    System.out.println("Sorted by Name : "+ sortedByName);
    

    Reverse Order

    List<String> reverseSortedByName = citiesName.stream().sorted((s1,s2)->s2.compareTo(s1)).collect(Collectors.toList());
    System.out.println("Reverse Sorted by Name : "+ reverseSortedByName );
    
    0 讨论(0)
提交回复
热议问题