Java 8 Sorting Using Two Fields

Based on some conditions, I read data from MongoDB and created List<Document>using a result set.

List<Document> documentList = new ArrayList<Document>();

An example entry is as follows:

documentList: [
    Document{
        { _id=5975ff00a213745b5e1a8ed9,
            u_id=,
            visblty = 1,
            c_id=5975ff00a213745b5e1a8ed8,                
            batchid=null,
            pdate=Tue Jul 11 17:52:25 IST 2017, 
            locale=en_US,
            subject = "Document2"
        }     },
    Document{
        { _id=597608aba213742554f537a6,
            u_id=,
            visblty = 1,
            c_id=597608aba213742554f537a3, 
            batchid=null,
            pdate=Fri Jul 28 01:26:22 IST 2017,
            locale=en_US,
            subject = "Document2"
        }    } 
]

Using this documentList, I filter again using some conditions, and then I need to sort the filter record based on some conditions (which I will receive in the request).

List<Document> outList = documentList.stream()
                .filter(d -> d.getInteger("visblty") == 1
                && (!StringUtils.isEmpty(req.pdate())? (d.getDate(CommonConstants.PDATE).after(afterDate)): true) 
                && (!StringUtils.isEmpty(req.pdate())? (d.getDate(CommonConstants.PDATE).before(beforeDate)): true)
                .sorted().skip(4).limit()
                .collect(Collectors.toList());

Not sure how to sort (dynamically you need to change the sort order based on input, it looks like a " pdate by DESC" or " subject by ASC")

Like: "order by pdate DESC" or "order by pdate ASC"" or "order by subject DESC"

How to sort using the Comparator object of the Document class.

Note. I tried a couple of methods that people suggested, but I still haven't succeeded. Thank you in advance!

+4
3

:

List<Document> outList = documentList.stream()
                               .filter(....)
                               .sorted(Comparator.comparing(Document::getPdate)
                                                 .thenComparing(Document::getSubject))   
                               .parallel();  
+5

Java :

  • order by pdate

    Comparator.comparing(Document::getPDate)
    
  • order by subject

    Comparator.comparing(Document::getSubject)
    
  • order by pdate, subject

    :

    Comparator.comparing(Document::getPDate).thenComparing(Document::getSubject)

, reversed() , :

Comparator.comparing(Document::getPDate).reversed()
          .thenComparing(Comparator.comparing(Document::getSubject).reversed())

, thenComparing , :

  • - (, - -a Comparable)
  • (, Comparable, , .
+6

Instead, .sorted()try using the method .sorted(Comparator comparator).

You can create Comparator, used with .sorted(comparator), with Comparator.comparing():

Comparator.comparing(Function keyExtractor)
    .reversed()
    .thenComparing(Function keyExtractor)

eg:

List<Document> outList = documentList.stream()
         // do your filtering here
        .sorted(Comparator.comparing(Document::getPdate).reversed()
                .thenComparing(Document::getSubject))
        .skip(4)
        .limit()
        .collect(Collectors.toList());

In this example, you can use a method reference Document::getPdateand Document::getSubjectinstead of a lambda expression, for example d -> d.getPdate()andd -> d.getSubject()

+3
source

Source: https://habr.com/ru/post/1682835/


All Articles