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.
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
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.
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