0

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

4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Taywin
0

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

0

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

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

0

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.

0

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.

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.