Java Coding Interview Questions and Answers | Sorting using comparator | Code Decode | [Most Common]

Published: 25 May 2022
on channel: Code Decode
110,573
1.7k

In this video of code decode we have covered sorting using comparator which is common in Java coding interview question and answers for experienced

Udemy Course of Code Decode on Microservice k8s AWS CICD link:
https://openinapp.co/udemycourse

Course Description Video :
https://yt.openinapp.co/dmjvd

Write a Comparator in Java to compare two employees based upon name, age and Address?

This Programming Interview Question is very commonly asked for Experienced Java software developers.

In order to sort Employee object on different criteria, we need to create multiple comparators e.g. NameComparator, AgeComparator, and AddressComparator, this is known as custom sorting in Java.

This is different from the natural ordering of objects, provided by the compareTo() method of java.lang.Comparable interface.

Compare method returns
Negative if 1st parameter is 2nd parameter
Positive if 1st parameter is 2nd parameter
0 if 1st parameter is = 2nd parameter

This is also a very popular hackerrank problem which Interviewer asked you solved as a Experienced Java Developer

Ordering objects of user-defined classes is done using a comparator interface. A comparator object can compare two objects that belong to the same class. Compare obj1 and obj2 using the following function.

A user-defined class's objects are ordered using the Java Comparator interface.

The compare(Object obj1,Object obj2) and equals methods are both included in the java.util package (Object element).

It supports different sorting sequences, which means you can order the items by any data member, such as rollno, name, age, or anything else.

Most Asked Core Java Interview Questions and Answers :    • Core Java frequently asked Interview Quest...  

Advance Java Interview Questions and Answers :    • Advance Java Interview Questions  

Java 8 Interview Questions and Answers :    • Java 8 Interview Questions(New Features)  

Hibernate Interview Questions and Answers :    • Hibernate Interview Questions Java  

Spring Boot Interview Questions and Answers :    • Advance Java Interview Questions  

Angular Playlist :    • Angular Course Introduction || Angular 8  

SQL Playlist :    • SQL Interview Questions and Answers  

GIT :    • GIT  

Subscriber and Follow Code Decode
Subscriber Code Decode : https://www.youtube.com/c/CodeDecode?...
LinkedIn :   / codedecodeyoutube  
Instagram :   / codedecode25  

#codedecode #javacodinginterviewquestion #javainterviewquestion


On this page of the site you can watch the video online Java Coding Interview Questions and Answers | Sorting using comparator | Code Decode | [Most Common] with a duration of hours minute second in good quality, which was uploaded by the user Code Decode 25 May 2022, share the link with friends and acquaintances, this video has already been watched 110,573 times on youtube and it was liked by 1.7 thousand viewers. Enjoy your viewing!