A Employee Class where i want to sort there lists one by there salary and one by there name individually as per the requirement ? How we do it?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
A Employee Class where i want to sort there lists one by there salary and one by there name individually as per the requirement ? How we do it?
Hi,
I didn't get your question.Please post your code what you tried then explain what is your need.
Happy to Help
Thanks,
Welcome! Please read this topic to learn how to post code correctly and other useful info for new members.
Please give your threads better titles.
A Employee Class with fields of name , age , address, salary and want to sort the objects lists by there salary and also by there name (Two way sorting on a same custom class) using comparator or comparable? How we do it?
Comparable must be implemented into the class of the objects to sort, so you can have only 1 ordering with Comparable for a given type, that should be the "natural" ordering of the objects (the order on name of Employee can be seen as the "natural" order).
Comparator must be implemented in other, distinct, classes, so you can have N orderings with Comparator for a given type.
Please, see, documentation of Comparable/Comparator, implement these interfaces and for any precise doubts, ask.
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
Ganeprog (February 5th, 2014), jedmustdie (February 5th, 2014)
can u give me the example of implementing comparator with two ordering ?
In a very sketchy way:
public class Book implements Comparable<Book> { private String title; private String publisher; // ... constructor(s), getter/setter methods, toString(), etc. ... public int compareTo(Book other) { return getTitle().compareTo(other.getTitle()); } } public class BookPublisherComparator implements Comparator<Book> { public int compare(Book book1, Book book2) { return book1.getPublisher().compareTo(book2.getPublisher()); } }
If you a have a primitive field, obviously, you can't use compareTo (title/publisher are String, that is Comparable and has compareTo). For primitives, just use < , > , etc... (or wrap into Integer, etc... or use e.g. compare(int x, int y) of Integer available since Java 7).
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
sunilshiwankar (February 5th, 2014)