RE: Java: Using Comparable to Establish Natural Order

G'day,

Chances are your getting odd behaviour because the signed int is not big
enough to cater for the difference between the two arbitrary doubles.  A
safer way would be to return either -1, 0 or 1 depending on the comparisons
between the two calculated values.

For example:

Int result = 0;
If (val1 > val2) {
  Result = 1;
}
Else if (val1 < val2) {
  Result = -1;
}

Return result;

Change the 1 and -1 accordingly depending on the desired sort order.

Hope this helps.

Cheers.
AB

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jared Wright
Sent: Saturday, 29 November 2008 10:16 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: Java: Using Comparable to Establish Natural Order

Nope. Had migrated to other bits and pieces of code in the little bit of 
time I had on Thanksgiving. Would still appreciate any thoughts or 
suggestions you might have.

JW

Ken Perry wrote:
> Did you get this working I am just now catching up with mail.
>
> Ken
>
> -----Original Message-----
> From: programmingblind-bounce@xxxxxxxxxxxxx
> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jared Wright
> Sent: Thursday, November 27, 2008 12:28 PM
> To: programmingblind@xxxxxxxxxxxxx
> Subject: Java: Using Comparable to Establish Natural Order
>
> Hi all, I've used comparable before, but with this go 'round I have 
> found I must be missing something. I'm trying to use Comparable to 
> establish the natural order of an Item class. Each item has a cost, 
> size, and with these two values, the "value" of the item can be 
> calculated. I wish for this value metric to be the index for the items' 
> natural ordering, so priority queues and collection sorting methods will 
> use this to order item objects. With the below code, my items are all 
> returned to me from a priority queue simply in the reverse order they 
> were added to the queue. Suggestions aappreciated, and I'll give much 
> thanks on this of all days for any help! *grin* I am trying to have it 
> order items with highest values first.
>
> Best,
> JW
>
>
> // Class representing an item.
> class Item implements Comparable {
>     private double cost=0; private double size=0;
>     public Item(double dCost, double dSize) {
>         cost = dCost;
>         size = dSize;
>     }
>     public double getCost() {
>         return cost;
>     }
>     public double getSize() {
>         return size;
>     }
>     public double getValue() {
>         return cost/size;
>     }
>     public int compareTo(Object oItem) {
>         double oItemValue = ((Item)oItem).getValue();
>         double result = ((this.getValue()))-oItemValue;
>         return (int)result;
>     }
>     }
> __________
> View the list's information and change your settings at 
> http://www.freelists.org/list/programmingblind
>
> __________
> View the list's information and change your settings at 
> http://www.freelists.org/list/programmingblind
>
>
>   

__________
View the list's information and change your settings at 
http://www.freelists.org/list/programmingblind



__________ NOD32 3650 (20081128) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com


__________
View the list's information and change your settings at 
http://www.freelists.org/list/programmingblind

Other related posts: