• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Alphanumeric sorting

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to sort the Alphanumeric values like K5, k7, y10, z1 ?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you want them sorting? What order should they be in?
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
input values: y10, k7, k5, z1

after sorting output values: K5, k7, y10, z1
 
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always first letter is character, not number. not like 5X, 35A52
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what have you tried so far?
 
fred rosenberger
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds about right. So all you need to do is create that Comparator implementing class.
 
I was born with webbed fish toes. This tiny ad is my only friend:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic