• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how comparator works?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a very basic question with Comparator. Please see the code below:

In the above code, I guess the compare() method in class AgeComparator is called for sorting the data. I would like to know from which part of the code, the compare() method is called.Basically how the flow of the above program is happening? Please help.

Thanks and Regards,
Jayanth Mathavan


Edit by mw: Added Code Tags.
[ August 14, 2007: Message edited by: marc weber ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Jayanth,"

Please check your private messages by clicking on the "My Profile" link near the top of the page.
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays.sort(employee, new AgeComparator());
the Arrays class uses the AgeComparator to compare entries in the employee array 2 until it gets the order it wants.

If you run this in debug and place a breakpoint in the AgeComparator you will see it called over and over...
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To see what's being compared and when, you could add the following to the compare method...

Employee e1 = (Employee)emp1;
Employee e2 = (Employee)emp2;
System.out.println("Comparing " + e1.getName() + " and " + e2.getName());
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also take a look at the code of Arrays.sort, which comes with the JDK.
 
reply
    Bookmark Topic Watch Topic
  • New Topic