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: 3649
    
  17

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: 2216
    
    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: 3649
    
  17

Ohh, I see. Take a look at this:
Compare with your original code and tell us what went wrong.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Sorting Collection