• Post Reply Bookmark Topic Watch Topic
  • New Topic

number sorting having prefix letter  RSS feed

 
prithvi pandey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi team,

i had a list of some numbers having a prefix A like A12 , A55, A76 ,A111 ,A888, A88 ,A880 A111 , A11,A1

i need to sort this list so the result would be A1,A11,A12,A55,A76....

please suggest me how to do this. can i use arrays.sort method to achieve the same or any other way.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prithvi pandey wrote:can i use arrays.sort method to achieve the same or any other way.


Yes. The Arrays.sort() method will work, but you will need to provide a custom comparator. The default string comparator uses ASCII ordering, so "A2" comes after "A11", which is probably not what you want.

Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Those are not numbers, but Strings. You can sort them as Strings but beware: A10 will come before A9.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
548
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch
Those are not numbers, but Strings. You can sort them as Strings but beware: A10 will come before A9.

Hence the need for a custom comparator!

Prithvi: With a custom comparator, you get the two Strings as input and can have any logic you want.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!