• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sorting numbers using Compare method

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you do any manipulation of the String in the class? It appears that you are removing the last character.
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thanks for the reply. No there is no string manipulation, only string the objects such as (String)current.getElement().
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the code?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everyone, i padded out my numbers and they are in order now.
thanks!
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic