Results 1 to 2 of 2
  1. #1
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Using Comparable and Comparator interfaces

    Hi, I have an array of Java objects(for example: Employee). I need to sort this array based on any one of the data variable(for example: emp_name, desg, salary) in the Java class.

    I had done using Comparable and Comparator interfaces. But the problem here is need to handle both ascending and descending dynamically based on parameter passed.
    Can anyone help me in this?

    Thanks.

  2. #2
    lenny is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    You're on the right track, you have to use compareTo inside your Comparator class that you create.

    you have to have the compare( ) method this is where how you want to sort it.

Similar Threads

  1. How to search with a Comparator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:39 PM
  2. How to write your own Comparator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:38 PM
  3. Creating a Comparable object
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:38 PM
  4. interface Comparable<T> problem
    By Lennon-Guru in forum New To Java
    Replies: 3
    Last Post: 03-05-2008, 01:17 AM
  5. interfaces..
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 06:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •