tag:blogger.com,1999:blog-8712770457197348465.post7180066909008630849..comments2024-03-28T01:04:16.249-07:00Comments on Javarevisited: Java Comparator Example for Custom Sorting Employee by Name, Age and Salaryjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-8712770457197348465.post-33970295299076102112024-02-06T05:15:39.060-08:002024-02-06T05:15:39.060-08:00I have question please clarify . When we use
Coll...I have question please clarify . When we use <br />Collections.sort(employees, Comparator.comparing(Employee::salary));<br />That means now we do not need inner classes like SalaryComparator that implement comparator interface. Also in this case it's just enough if employee classs implements Comparable interface right ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-38953299408989459622016-12-08T17:18:43.410-08:002016-12-08T17:18:43.410-08:00Hello @Kanhu, are they after a Comparator which fi...Hello @Kanhu, are they after a Comparator which first sort on ID and then on NAME? e.g. if two employee have same id then check their name? if yes then it's possible, you can do that in compare() method, just check if id is equal first if they are then check if name is equal then return 0 or 1 or -1 accordingly e.g.<br /><br />if(obj1.id === obj2.id){<br /> if(obj1.name.equals(obj2.name)){<javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-31950047147999339042016-12-08T17:13:36.057-08:002016-12-08T17:13:36.057-08:00@Anonymous, when you call the sort method, you giv...@Anonymous, when you call the sort method, you give a list of employee and a Comparator to compare them. The Java uses a modified version of Mergesort to sort the list of employee and for comparing two employee it uses the Comparator you have provided. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-46143321512699716882016-12-07T02:18:19.931-08:002016-12-07T02:18:19.931-08:00Collections.sort(listOfEmployees, Employee.NameCom...Collections.sort(listOfEmployees, Employee.NameComparator);<br /><br />can you explain the above code?<br />what happens when i call sort method, how does the flow goesAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-70368892031648009432016-12-06T19:34:05.657-08:002016-12-06T19:34:05.657-08:00Hello friends, I was asked a question in an interv...Hello friends, I was asked a question in an interview regarding comparator interface, they ask me how to sort employee id & employee name using only one comparator class. you can't create two separate comparator class for id comparator & name comparator.Sorting must be customized sorting order. Please one body answer me.Anonymoushttps://www.blogger.com/profile/12988909333628096651noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11823070488272665262016-02-14T23:48:07.172-08:002016-02-14T23:48:07.172-08:00Hello Javin. Recently i was asked in interview. Su...Hello Javin. Recently i was asked in interview. Suppose i have a list of heterogeneous objects e.g. Student,employee,person,etc. Each having a common property firstName, how to write a comparator for it and it should not do any instanceOf checking.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-76490822510116308082015-04-13T11:00:55.446-07:002015-04-13T11:00:55.446-07:00Great article. I never thought of using anonymous ...Great article. I never thought of using anonymous class comparator to solve many sorting criteria within pojo, thanks a bunch.Anonymoushttps://www.blogger.com/profile/03057270832088817227noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-67081548639037054482015-04-03T19:56:40.979-07:002015-04-03T19:56:40.979-07:00@Chandra, look at closely mate, we are using Anony...@Chandra, look at closely mate, we are using Anonymous class, which means a class which implements Comparator interface and that's why we are able to create instance using new() keyword. The logic of implementation is always in the overridden compare() method. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61751025458011713282015-04-03T18:22:25.164-07:002015-04-03T18:22:25.164-07:00As Comparator is an interface, how we can create a...As Comparator is an interface, how we can create an object for Comparator using new Keyword.<br /><br />public static final Comparator DOJComparator = new Comparator()<br /><br />Anonymoushttps://www.blogger.com/profile/04243933782210811447noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-92033468295257128692015-02-02T09:22:50.130-08:002015-02-02T09:22:50.130-08:00Nice article, but there are some mistakes in examp...Nice article, but there are some mistakes in example. Syntax of compare() and compareTo() are not correct. For Example, Instead of public int compare(Employee o1, Employee o2) , It should be public int compare(Object o1, Object o2)<br />Please correct the example.<br />Instead ofAnonymoushttps://www.blogger.com/profile/01331273374564091956noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65880157100369190422014-12-16T00:45:08.192-08:002014-12-16T00:45:08.192-08:00From Java 8 onwards you can use lambda expression ...From Java 8 onwards you can use lambda expression instead of anonymous class to implement custom comparator. You can also use method reference and some useful methods like Comparator::comparing() to sort with multiple attributes. <br /><br />For example, your code for sorting employees based upon salary can be re-written in Java 8 as follows :<br /><br />Collections.sort(employees, Anuragnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32117866931165095272014-11-10T02:55:29.328-08:002014-11-10T02:55:29.328-08:00How to run this programHow to run this programAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-25589038019301808772014-01-30T19:16:37.058-08:002014-01-30T19:16:37.058-08:00A small correction - Instead of "Though both ... A small correction - Instead of "Though both compare() and compareTo() method looks similar they are different in a sense that, FORMER accept one parameter, while LATER accept two parameter."<br />, it should be former (compare())accept two parameters and later(compareTo()) accepts one parameter<br /><br />Shamhttps://www.blogger.com/profile/01391061245512790769noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-71924028000803353002014-01-27T13:46:16.123-08:002014-01-27T13:46:16.123-08:00Equals can not be simplier?Equals can not be simplier?Anonymoushttps://www.blogger.com/profile/15336422835179942938noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-15308170832408769262014-01-26T14:02:24.260-08:002014-01-26T14:02:24.260-08:00Nice article , but there are not static nested cla...Nice article , but there are not static nested class(NameComparator, AgeComparator, SalaryComparator and DOJComparator), I would say there are static anonymous class.Anonymoushttps://www.blogger.com/profile/14089962966332597302noreply@blogger.com