• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which would come first in a sort: 01 or 1?  RSS feed

 
Toni Howlett
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im planning to have a number 00 to 99 (prob won't get close to 99 ever... -beside the point tho) infront of a string.
ex
00hello
01goodbye
02something
04weeee
...
10numten


If I was to have these in random order and sort them to appear in the above order... would 01 come before 10?

I want to say yes, but I can't manage to convince myself -.-
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is....

it depends.

it depends on how the sort (or the comparater) was implemented. The better questions is: What do you WANT to happen?
 
Toni Howlett
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want it to come out sorted 00,01, 02,03...10,11,12 etc

Just want to make sure using a 0 as padding for the first 10 digits won't throw off the sort
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think 01 should come first then 10 because in Unicode we can see that 0x0030=0 and 0x0031=1
so zero come first..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Toni Howlett wrote:Im planning to have a number 00 to 99


No, you don't. You either have a String that's going from "00" to "99" or you have a number that's going from 0 to 99.

And you can sort it however you choose to, by supplying the appropriate Comparator.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!