Am trying to sort the filed in filter.
Input Document / Sample Record:
DocumentList: [
Document{
{
_id=5975ff00a213745b5e1a8ed9,
u_id=,
mailboxcontent_id=5975ff00a213745b5e1a8ed8,
idmapping=Document{
{ptype=PDF, cid=00988, normalizedcid=00988, systeminstanceid=, sourceschemaname=, pid=0244810006}
},
batchid=null,
pdate=Tue Jul 11 17:52:25 IST 2017, locale=en_US
}
},
Document{
{
_id=597608aba213742554f537a6,
u_id=,
mailboxcontent_id=597608aba213742554f537a3,
idmapping=Document{
{platformtype=PDF, cid=00999, normalizedcid=00999, systeminstanceid=, sourceschemaname=, pid=0244810006}
},
batchid=null,
pdate=Fri Jul 28 01:26:22 IST 2017,
locale=en_US
}
}
]
Here, I need to sort based on pdate.
List<Document> outList = documentList.stream()
.filter(p -> p.getInteger(CommonConstants.VISIBILITY) == 1)
.parallel()
.sequential()
.collect(Collectors.toCollection(ArrayList::new))
.sort()
.skip(skipValue)
.limit(limtValue);
Not sure how to sort
"order by pdate DESC"
Thank you in advance!
You can use .sorted() Stream API method:
.sorted(Comparator.comparing(Document::getPDate).reversed())
And the full, refactored example:
List<Document> outList = documentList.stream()
.filter(p -> p.getInteger(CommonConstants.VISIBILITY) == 1)
.sorted(Comparator.comparing(Document::getPDate).reversed())
.skip(skipValue).limit(limtValue)
.collect(Collectors.toCollection(ArrayList::new))
Few things to remember about:
List implementation, use
Collectors.toList()collect() is a terminal operation and should be called as the last operation.parallel().sequential() this is totally useless - if you
want to parallelize, stick to .parallel() if not, do not write
anything, streams are sequential by defaultIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With