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.

Results 1 to 7 of 7

Thread: Help with sorting Employee class

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with sorting Employee class

    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?


  2. #2
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default re: Help with sorting Employee class

    Hi,

    I didn't get your question.Please post your code what you tried then explain what is your need.

    Happy to Help

    Thanks,

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default re: Help with sorting Employee class

    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.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with sorting Employee class

    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?

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Help with sorting Employee class

    Quote Originally Posted by sunilshiwankar View Post
    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.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. The Following 2 Users Say Thank You to andbin For This Useful Post:

    Ganeprog (February 5th, 2014), jedmustdie (February 5th, 2014)

  7. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with sorting Employee class

    can u give me the example of implementing comparator with two ordering ?

  8. #7
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Help with sorting Employee class

    Quote Originally Posted by sunilshiwankar View Post
    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.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  9. The Following User Says Thank You to andbin For This Useful Post:

    sunilshiwankar (February 5th, 2014)

Similar Threads

  1. Simple Employee Class Not Passing Tests
    By floorplay in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 20th, 2013, 05:10 AM
  2. The Employee class
    By vercammen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2013, 02:27 AM
  3. Employee Class Exercise... Code will not run!!!!
    By jbarcus81 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: January 26th, 2012, 03:39 PM
  4. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM

Tags for this Thread