Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

Alphanumeric sorting

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1361
How to sort the Alphanumeric values like K5, k7, y10, z1 ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4102

2

How do you want them sorting? What order should they be in?
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1361
input values: y10, k7, k5, z1

after sorting output values: K5, k7, y10, z1
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10519

10

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1361
Always first letter is character, not number. not like 5X, 35A52
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And what have you tried so far?

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10519

10

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

Joined: Apr 08, 2004
Posts: 1361
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

Joined: Oct 27, 2005
Posts: 19413

13

Sounds about right. So all you need to do is create that Comparator implementing class.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions

subject: Alphanumeric sorting
Similar Threads
Regex
w00t: Word of the Year 2007
Java Object Reference Variable Question
Sorting Query problem in MySQL
Generating captcha (alpha numeric) in java