This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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


Win a copy of Head First Android this week in the Android forum!
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: 3892
    
  18

Why not simply the following:


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
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: 2354
    
    8
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: 3892
    
  18

Ohh, I see. Take a look at this:
Compare with your original code and tell us what went wrong.
 
wood burning stoves
 
subject: Sorting Collection
 
It's not a secret anymore!