This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Sorting Collection  RSS feed

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 9132
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not simply the following:
 
Arun Prasath
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Rancher
Posts: 2933
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I would be adding null also..



Ok, then you need to check for null value first before invoking the compartTo(...) method.
 
Stephan van Hulst
Saloon Keeper
Posts: 9132
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh, I see. Take a look at this:
Compare with your original code and tell us what went wrong.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!