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

# Sorting

Ranch Hand
Posts: 134
• Number of slices to send:
Optional 'thank-you' note:

Can anyone explain the logic of sorting here by using s2.charAt(1)-s1.charAt(1) as coded above please. Thanks in advance.

Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:
By adding some println statements, the code will explain itself...

[ July 29, 2007: Message edited by: marc weber ]

Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:
So it seems the logic is to compare the first letter of the compared strings and return a positive number if the s2 starts with a "higher" (in the alphabet) letter, a negative number if s1 starts with a "higher" letter, and 0 if they are they same.... right?

Ranch Hand
Posts: 893
• Number of slices to send:
Optional 'thank-you' note:
That's not true.

s1.charAt(1) retrieves the second char, because this method is zero based.

jibs parap
Ranch Hand
Posts: 134
• Number of slices to send:
Optional 'thank-you' note:
Thanks Remco. That was interesting to see.
But hows the array sorted based on the return value. The final array is {Good, Ugly, Bad} after sorting.

jibs parap
Ranch Hand
Posts: 134
• Number of slices to send:
Optional 'thank-you' note:
Sorry, I was a bit drowsy, when sorted it last.
Now sorted perfect.

Cheers guys

 I was born with webbed fish toes. This tiny ad is my only friend: a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com