Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unable to sort Alphanumeric values

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

I had a small query regarding the following

When sorting the following values by .sort() it is not displaying the values in ascending order i.e
it is taking 10 as 1 and displaying 10 first rather than 2

s1[0]="BS EN 60704-2-10:2002";
s1[1]="BS EN 60704-2-2:2003";

Thanks in Advance
saiprasanna
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because this compares every characters when sorting.
You should make your own Comparator and break the string into relevant pieces.
 
sai prasanna
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Satou
 
sai prasanna
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but the values are in different format as follows then also should i write my own comparator

BS EN 60704-1:1995

BS 1016-104.4:1998, ISO 1171:1997

BS 2782-3:Method 323B:1996, ISO 6721-5:1996

thanks in advance
saiprasanna
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sai prasanna:
but the values are in different format as follows then also should i write my own comparator

BS EN 60704-1:1995

BS 1016-104.4:1998, ISO 1171:1997

BS 2782-3:Method 323B:1996, ISO 6721-5:1996

thanks in advance
saiprasanna



Yes. If the standard String compare method doesn't give you the ordering you want then write a Comparator that does.

How about this for the custom method:
  • Use String.split() to get a String array for each long String to be compared. For instance, the two from your initial post would be "BS", "EN", "60704", "2", "10", "2002" and "BS", "EN", "60704", "2", "2", "2003".
  • In a loop, compare corresponding elements in each array.

  • - Try converting the given element in each array to an int. If neither conversion throws a NumberFormatException, compare the two ints; else compare them as strings.
    As soon as a comparison provide a lt, eq, gt decision you're done. If all corresponding parts are equal and neither array has extra pieces, the two are equal.

    Note: This would ignore differing puncuation/spacing. "BN 123" would be equal to "BN:-:===123". To change this you could include Strings of delimiters in your array. For instance, those two might be "BN", " ", "123" and "BN", ":-:===", "123".

    Cool?

    STANDARD NOTE ABOUT equals() AND hashCode():
    If you create a custom class for these numbers and override the equals() method, make sure that hashCode() returns equal ints for any two objects that are equal. i.e. if a.equals(b) then a.hashCode() had darn well better == b.hashCode().
    [ September 07, 2006: Message edited by: Ryan McGuire ]
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic