• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

treeset sort

TreeSet t=new TreeSet();
t.add("A");
t.add("Z");====>"Z".compareTo("A");(positive) how much positive number it comes?
t.add("A");====>"A".compareTo("B");(negative)//here while jvm doing default natural sorting i thought instead of B, Z has to be compared with A but B is being compared with A since B is root element. What is meaning of root element. why Z is not root element??
System.out.println(t)//comes [A,B,Z} right not [Z,B,A]
how to get custom sorting like Z B A

why StringBuffer does not support default natural sorting. How to sort String Buffer objects.

obj1.compareTo(obj2)//obj2 is already present element in tree set and obj1 is new element being added??

please advise
0
gudii9
Asked:
gudii9
2 Solutions
 
Jacques Bourgeois (James Burger)PresidentCommented:
I am not a Java programmer, so I cannot give you sample code. But in most of the languages I have worked with, in most situations, it is easier to sort the Data before adding it to any container that does not have a Sort method.

Build the data in a sortable array or collection.

Sort it and then fill your Treeset with the resulting set of data.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
to any container that does not have a Sort method
The TreeSet does not have a sort method because it intrinsically maintains the a sort order. So NO, don't put it in any intermediate data structure to then fill your TreeSet, that will just confuse the issues.

"Z".compareTo("A");(positive) how much positive number it comes?
It doesn't matter what the actual value is, just that it is positive. You are wasting your own time by worrying about such things. If you must, just find out for yourself, ie. System.out.println("Z".compareTo("A"));

t.add("A");====>"A".compareTo("B");(negative)//here while jvm doing default natural sorting i thought instead of B, Z has to be compared with A but B is being compared with A since B is root element. What is meaning of root element. why Z is not root element??
You may need to clarify what is happening here and what your real question is. You talk about "B" but there is no B in your set. Did you mean to do.... t.add("B");    (instead of trying to add A again). And what do you mean by root element? While yes, since it is a tree, the internal implementation may have a root element, but you would have no view of that in code that is using the TreeSet.

I think you get too concerned with the internal working of some of these things, but it gives you no real benefit at all to know what is going on. All you should be concerned with is that in this case, the TreeSet will maintain the natural sort order of the Strings that you add to it, by utilising String.compareTo() method.


System.out.println(t)//comes [A,B,Z} right not [Z,B,A]
how to get custom sorting like Z B A
This has been covered MANY times in your previous questions... Write a class that implements the Comparator interface, have it's compare method return the opposite of what a normal String compareTo method does (you want the opposite so that you get your "reverse" ordering), and then pass an instance of your class to the constructor of the TreeSet when you create it.


why StringBuffer does not support default natural sorting. How to sort String Buffer objects.
Why? Most probably because a StringBuffer can change over time. Consider this... You have a StringBuffer object, sb1, which holds "A" and one, sb2, that holds "B". Say that StringBuffer did support a default natural sort order and you added these two to a TreeSet, then sb1 would be first and sb2 would be second in the TreeSet. Now say, you did a sb1.insert(0, "Z"), which inserts Z at the beginning, resulting in a String representation which is "ZA". The TreeSet has no idea that something internal to the StringBuffer changed so it's ordering is still sb1 first and sb2 second, but that looks like "ZA", "B" which is NOT the natural sort order.

To summarise the above, any object which can internally change (what is called a "mutable" object) means that it's position in any natural sort order can change and so generally they won't implement Comparable.

 How to sort them? Well you could (by doing as above and implementing a Comparator) but for the reasons just given, I wouldn't do it. Just add the actual Strings to the TreeSet to sort those.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now