• 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

The compare() method

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have an array
viz..int[] a={1,5,3,7,6,8,9};
now i want to sort the priority of the elements in teh array so that they culd be stored in a priority quue


i don't get how the compare method is assigning the priorities!!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comparing in Java (with Comparable or Comparator) works as follows:
- if the return value is negative, the first value is considered smaller than the second
- if the return value is positive, the first value is considered larger than the second
- if the return value is 0, the values are considered to be equal.

So let's apply this to your example. Let's take 5 and 6. Compare(5, 6) will return 1, so 5 will be larger than 6. As a result, 5 will be put closer to the end than 6.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another problem with your compare() method. It will work as long as the parameter values remain within a restricted range, but you need to consider what happens if the values -1234567890 and 1234567890 are compared. Try it and see!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You also really don't need to write your own comparator for ints. There is an )]Arrays.sort(int[]) method that takes care of it for you.

And if you have Integers, Integers are Comparable as well, so you could just Arrays.sort(Object[]) them.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but this is probably a class exercise where they have been told to implement a compare() method . . .

And congratulations on the "Rancher" handle.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to sort the date fields...

for eg,

02/22/2009", "09/22/2007", "08/22/2008

if we declare string,
String[] names = {"02/22/2009", "09/22/2007", "08/22/2008"};

Then it will return as,

[02/22/2009, 08/22/2008, 09/22/2007].


so how to sort the date.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, write your own Comparator that uses SimpleDateFormat and Date. You then compare the Date objects.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Yes, but this is probably a class exercise where they have been told to implement a compare() method . . .



Woops, sorry, I didn't get that.

Originally posted by Campbell Ritchie:
And congratulations on the "Rancher" handle.



Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic