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....

7 Years
Discussion Span
Last Post by Taywin

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


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. :(

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.

Edited by Taywin: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.