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
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.