Hi all,

I am new in java. How can I sort a linked list on the basis of a particular string..?

I have created a class with two elements,

1. Name
2. Address

and I am reading these elements many times

Now I want to sort this on the basis of name..

Please help me .. Thanks in advance....

Usual way is to add a compareTo method to your class. Inside that method you code whatever logic you want to compare two instances (eg compare name fields).
Have a look at the Comparable interface in the Java API

Adding to that.......


You can overload the operators ==, < and > for the class.

You can overload the operators ==, < and > for the class.

Would you please give an example or reference material for doing that in Java?

Adding to that.......


You can overload the operators ==, < and > for the class.

Sorry about that. I replied as though in C++.

Please ignore the post.

Hmm... That's one thing I missed from C++ in Java. :(

@BroonoVilliam
If you are allowed to use compareTo(), then go for it as JamesCherill said. Otherwise, you may have to implement your own string comparison method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.