This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Alphanumeric sorting  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to sort the Alphanumeric values like K5, k7, y10, z1 ?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you want them sorting? What order should they be in?
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
input values: y10, k7, k5, z1

after sorting output values: K5, k7, y10, z1
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It helps if instead of providing a single, simple example, you actually write out how they should be sorted. Your example does tell me how to sort values like 5X, m04, 35A52, etc.

But in BROAD terms, you can write your own comparator. This is a special class where you pass it two 'things', and it tells you if the first is greater than, less than, or equal to the second. But YOU have to write that logic.
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always first letter is character, not number. not like 5X, 35A52
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what have you tried so far?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:Always first letter is character, not number. not like 5X, 35A52

That's great...but still not enough detail...

Can there be two letters? three?

how many digits?

Can there be more than one set of letters and/or digits?

again...YOU need to define how to sort them. We can't tell you how to do it, because we don't know. If you can tell us, we can help you write the code. If you don't tell us, there is nothing we can do but continue to make wild guesses, which you simply say "nope, that doesn't work because", and then we repeat the process.
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first character is always letter, from second place onwards number/number(s). d1 / d12
I am planning to use Comparator Interface with compare() method and in the TreeSet<Object> ts = new TreeSet<Object>(new comparator() );

Is it right approach ? Any other input ?
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds about right. So all you need to do is create that Comparator implementing class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!