Sorting with Comparable and Comparator in Java.Bengali-Ep:32

Опубликовано: 03 Август 2026
на канале: Ajoy Debnath
180
9

Hi,
Hope you are all doing great. Let's learn together.
Join Telegram: 📲 https://t.me/contactajoydebnath

Chapters ❤
0:00 Introduction
00:21 Theory & A Sorting Scenario implement
04:59 How do the sort() method of Collections class work?
07:29 Implementing a scenario using Comparable interface
20:55 Comparable interface theory
23:02 Comparable interface code implement
38:32 Implementing a scenario using Comparator interface
42:41 Comparator interface theory
45:20 Comparator interface code implementation


Document Link :
https://docs.google.com/document/d/1t...

Note:
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak. Since Oak was already a registered company, James Gosling and his team changed the name from Oak to Java.

STS Link:
https://spring.io/tools

JDK Link:
https://www.oracle.com/java/technolog...

Video Links:
Core Java Playlist:
   • Core Java  

Spring Playlist :
   • Spring Framework  

Java Videos Links :
Guide To Java 8 Optional Class. Part-II. --    • Guide To Java 8 Optional Class. Part-II Be...  
Guide To Java 8 Optional Class. Part-I. --    • Guide To Java 8 Optional Class. Part-I. Be...  
Stream API : Intermediate & Terminal operations. --    • Stream API : Intermediate & Terminal opera...  
Stream API: Intermediate & Terminal operations. --    • Stream API: Intermediate & Terminal operat...  
Factory Method Design Pattern. --    • Factory Method Design Pattern. Bengali-Ep:...  
How to create an Immutable class in Java? Immutable Design Pattern. --    • How to create Immutable class in Java? Imm...  
Object Cloning, Shallow Copy, and Deep Copy. --    • Object Cloning, Shallow Copy, and Deep Cop...  
Exception, Custom Exception, throw, throws --    • Exception, Custom Exception, throw, throws...  
Excetption, try, catch, finally --    • Exception, Propagation try, catch, finally...  
Exception, Exception Hierarchy, Call Stack, Propagation. Bengali-Ep:22 --    • Exception, Exception Hierarchy, Call Stack...  
Lambda Expression, Functional Interfaces, Anonymous Class. --    • Lambda Expression, Functional Interfaces, ...  
Singleton Pattern. Lazy Instantiation for Multiple Threads.Part-2 Bengali-Ep:20    • Singleton Pattern. Lazy Instantiation for ...  
Singleton Design Pattern in Java. Early & Lazy Instantiation Bengali-Ep:19 :    • Singleton Design Pattern Java. Early & Laz...  
HashSet, Its Methods & How It Works Internally in Java :    • HashSet, Its Methods & How It Works Intern...  
HashSet & How It Works Internally in Java. --    • HashSet & How It Works Internally in Java....  
LinkedList,Methods & How It Works Internally in Java. --    • LinkedList, Methods & How It Works Interna...  
ArrayList, ListIterator, How It Works Internally --    • ArrayList, ListIterator, How It Works Inte...  
Collection Framework ----    • Collections Framework, Collection & Iterat...  
'super' keyword -    • 'super' keyword in Java. Bengali-Ep:13  #b...  
'this' keyword -    • 'this' keyword in Java. Bengali-Ep:12  #be...  
Final Keyword -    • Final keyword, Final variable, method, cla...  
Static Keyword, Static variable, block, method, nested class -    • Static keyword, Static variables, block, m...  
Access Modifier. Private, Public, Default, Protected --    • Access Modifier. Private, Public, Default,...  
Encapsulation, Data Hiding, Setter-Getter method, JavaBean Class --    • Encapsulation, Data Hiding, Setter-Getter ...  


Spring Video Links:
JDBCTemplate, RowMapper, ResultSetExtractor, BeanPropertyRowMapper Spring --    • JDBCTemplate, RowMapper,ResultSetExtractor...  
Spring JDBCTemplate, JDBC, Statement, and PreparedStatement --    • Spring JDBCTemplate, JDBC, Statement and P...  

Like, Comment, Share, and Subscribe.
Thank you again.

How does the sort() method of the Collections class work?
The Collections.sort has two overloaded methods :
public static void sort(List list): Sort the list into ascending order, the natural ordering of its element.
public static void sort(List list, Comparator c): Sort the list according to the specified comparator.
Internally the Sort method does call Compare method of the classes it is sorting. To compare two elements, it asks “Which is greater?” Compare method returns :
If both the objects are equal, returns 0
If the first object is greater than the second, return a value greater than 0
If the second object is greater than the first, returns a value less than 0

Java provides two interfaces to sort objects using data members of the class:
Comparator
Comparable

Comparator :
A comparator interface is used to order the objects of a user-defined class. This interface is present in java.util package and contains 2 methods compare(Object obj1, Object obj2) and equals(Object element). Using a comparator, we can sort the elements based on data members.

Comparable:
The Comparable interface is used to compare an object of the same class with an instance of that class, it provides the ordering of data for objects of the user-defined class. The class has to implement the java.lang.Comparable interface to compare its instance, it provides the compareTo method that takes a parameter of the object of that class.
The Comparable interface contains the method compareTo decide the order of the elements.