aspose file tools*
The moose likes Beginning Java and the fly likes The compare() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The compare() method" Watch "The compare() method" New topic
Author

The compare() method

geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78
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!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Yes, but this is probably a class exercise where they have been told to implement a compare() method . . .

And congratulations on the "Rancher" handle.
raju raj
Greenhorn

Joined: Sep 15, 2008
Posts: 1
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

Joined: Oct 27, 2005
Posts: 19719
    
  20

Again, write your own Comparator that uses SimpleDateFormat and Date. You then compare the Date objects.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The compare() method