Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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: 3995
    
  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: 2373
    
    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: 3995
    
  18

Ohh, I see. Take a look at this:
Compare with your original code and tell us what went wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting Collection
 
jQuery in Action, 3rd edition