wood burning stoves 2.0*
The moose likes Java in General and the fly likes Sorting Collection 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 Collection" Watch "Sorting Collection" New topic
Author

Sorting Collection

Arun Prasath
Ranch Hand

Joined: May 23, 2007
Posts: 80
Hi All,

Could you suggest some better solution to sort the collection ?

The collection containts the objects (code below) having only one property which can have any of "Number", "Text/String", "Alpha numeric".




I tried the following comparator, it is not effective ..!



Method to test





Please suggest some better idea..


Thanks,
Arun.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

Why not simply the following:
Arun Prasath
Ranch Hand

Joined: May 23, 2007
Posts: 80
Hi Stephan,

I would be adding null also.. So your code will result in null pointer expression.

i am expecting the sorting result something like this,

null
null
-123
-20
-4
0
1
2
12
20
1abc
five
four
three


Please provide some better idea..
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
I would be adding null also..


Ok, then you need to check for null value first before invoking the compartTo(...) method.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

Ohh, I see. Take a look at this:
Compare with your original code and tell us what went wrong.
 
jQuery in Action, 2nd edition
 
subject: Sorting Collection
 
Similar Threads
determine the input type of the value of the function parameter map
need to find quicksort that sorts in descending order ..ARG
comparator example needed
Comparator and Comparable
Sorting the Rows in a JTable Component Based on a Column