Win a copy of Head First Android this week in the Android forum!
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:
• Tim Cooke
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Rob Spoor
• Bear Bibeault
Saloon Keepers:
• Jesse Silverman
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Al Hobbs
• salvin francis

# TreeSet should output in ascii order?

Ranch Hand
Posts: 140
• Number of slices to send:
Optional 'thank-you' note:
This is a Whizlabs example to prove the output sort sequence of a TreeSet. It outputs: "JSP Java Swing XML" - the explanation is that this is ascii value order, but its not! XML(241) and should come after JSP(237). It looks like a letter by letter ascii comparison rather than a total ascii value, is that right?

Whizlabs test program:

To prove it programmatically (because I need practice on collections):

This outputs:
LinkedHashMap output, to prove ascii ordering of TreeSet:
195: AAA
386: Java
241: XML
520: Swing
237: JSP

Second question:
The LinkedHashMap output is in key input sequence, how can I output it in key sequence, in this case by numeric magnitude?

Thank you!

author
Posts: 23909
142
• Number of slices to send:
Optional 'thank-you' note:

Nigel Shrin wrote:This is a Whizlabs example to prove the output sort sequence of a TreeSet. It outputs: "JSP Java Swing XML" - the explanation is that this is ascii value order, but its not! XML(241) and should come after JSP(237). It looks like a letter by letter ascii comparison rather than a total ascii value, is that right?

While I agree that maybe the wording could be clearer, I have to say to whizlabs defense, that no-one ever takes a sum of all the ASCII characters to determine order. Sorting by the sum of the ascii values is pretty useless, and just not done.

Henry

Nigel Shrin
Ranch Hand
Posts: 140
• Number of slices to send:
Optional 'thank-you' note:
Thank you Henry

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

Henry is absolutely correct. Great insight Henry.

Best Regards,

 Consider Paul's rocket mass heater.