File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Sorting numbers using Compare method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting numbers using Compare method" Watch "Sorting numbers using Compare method" New topic
Author

Sorting numbers using Compare method

Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
Hello,

I have implemented a Doubly Linked List to sort elements (integer or character) into ascending order. My problem is that it reads 34 as 3, 56 as 5, 78 as 7 and so on. It also behaves the same for letters like AB is A and TY is T...etc.
I have converted the object elements into strings so that i can use the compare method like this:



where the current is the DLNode current node and next is the DLNode next node after current node.

Can anyone figure out why it's doing this?

Thanks in advance.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Do you do any manipulation of the String in the class? It appears that you are removing the last character.
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
Hi,
thanks for the reply. No there is no string manipulation, only string the objects such as (String)current.getElement().
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Can you post the code?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Is this the kind of thing you get?

If so, that is the proper sort order for strings, but not for numbers. You have to right justify strings that are numbers to make them sort in numeric order. I used zeros to pad - spaces also work but they're a bit harder to see.

So your choices are to right justify strings or convert strings to some numeric type like Integer. One challenge with right justification is making sure your numbers are wide enough for the longest possible value. For positive ints that might be 10 digits. See what this line does:


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
You could also write your own Comparator to order the Collection.

For instance:



The output is: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Regards,
Edwin Dalorzo
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
thanks everyone, i padded out my numbers and they are in order now.
thanks!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The custom comparator is a pretty good idea. I just got tired of typing before I got to it. (Honest!) It has more overhead doing two conversions on every compare, but it's reusable in other future projects is a more "correct" solution.
 
wood burning stoves
 
subject: Sorting numbers using Compare method