sort using comparators in java 8

aditya chaudhari
JavaDeveloperDiary — JDD
1 min readOct 3, 2021
comparator using java 8

comparator is powerful technique to implement custom sort in java.
Before java 8 usual way was to use anonymous inner class.

In this post we ‘ll see using comparator before and after java 8 .
Code for reference :

java 8 comparator
  • using anonymous class for sorting
    refer line 26, this is traditional way to use anonymous inner class to write comparators
  • using java8 for sorting
    refer : 48 we can use stream and then sorted method to sort this also require us to pass comparator object.
  • ascending and descending sorts
    please refer lines : 46, 52
  • chain of sorting
    refer :58 , we can chain multiple sorting this is great feature to add chain of custom sorting.

If you have any suggestions regarding topic or post please let me know in comments . If you like content please encourage me to write more by applause on this post. Thanks for reading.

All java 8 related articles on this publication can be found here.

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT