• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeSet should output in ascii order?

 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Henry is absolutely correct. Great insight Henry.

Best Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic