| Author |
TreeSet should output in ascii order?
|
Nigel Shrin
Ranch Hand
Joined: May 18, 2009
Posts: 119
|
|
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!
|
Nigel
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16681
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Nigel Shrin
Ranch Hand
Joined: May 18, 2009
Posts: 119
|
|
|
Thank you Henry
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
Hi,
Henry is absolutely correct. Great insight Henry.
Best Regards,
|
Prithvi/Beenish,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
|
 |
 |
|
|
subject: TreeSet should output in ascii order?
|
|
|