Results 1 to 9 of 9
  1. #1
    Chinnu55 is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default Sorting Arraylist objects with multiple fields - Please help

    Hi,

    I have a ArrayList which contains list of employee objects like

    Employee object contains name, dept and designation.

    ArrayList al --> employee [object1]
    |_ James, D1, manager
    employee [object2]
    |_ Dave, D2, sales manager
    employee [object3]
    |_ Roger, D1, accounts manager
    employee [object4]
    |_ Lisa, D3, engineer
    employee [object5]
    |_ Angela, D2, PM
    employee [object6]
    |_ Mike, D1, manager

    Now, I want to sort them by designation first and then by department.

    i.e. first sort by designation

    Roger, D1, accounts manager
    Lisa, D3, engineer
    James, D1, manager
    Mike, D1, manager
    Angela, D2, PM
    Dave, D2, sales manager

    then by department,

    Roger, D1, accounts manager
    James, D1, manager
    Mike, D1, manager
    Angela, D2, PM
    Dave, D2, sales manager
    Lisa, D3, engineer

    Please let me know how to achieve and code sample will be great help.

    Thanks in advance,
    Chinnu

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You want to write comparators for you Employee class, and pass it into the Collections.sort method.

    Comparator (Java Platform SE 6)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    That's actually sorting by department then designation then name.

  5. #5
    Chinnu55 is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    Thanks for replying...

    I wanted to sort the ArrayList twice i.e. first time by designation and again by department. Due to the confusion I removed Name field.

    for ex:
    Department Designation

    D1 accounts manager
    D1 director
    D1 manager
    D3 engineer
    D2 PM
    D2 sales manager

    If you see above structure, Designation is sorted i.e. under D1 all the employees designation is sorted but, Department is not sorted.

    Now, we need to sort Department too

    Department Designation
    D1 accounts manager
    D1 director
    D1 manager
    D2 PM
    D2 sales manager
    D3 engineer

    Now, Department is sorted.

    I was thinking to add 2 inner classes that implements Comparator

    Java Code:
        class DesignationComparator implements Comparator {  
    	    public int compare(Object o1,Object o2) {  
    	    	String strObj1 = ((EmployeeDataBean)o1).getEmployeeDesignation();
    			String strObj2 = ((EmployeeDataBean)o2).getEmployeeDesignation();
    			return strObj1.toUpperCase().compareTo(strObj2.toUpperCase());
    		 }
    	}
    	
    	class DepartmentComparator implements Comparator {  
    	    public int compare(Object o1,Object o2) {  
    	    	String strObj1 = ((EmployeeDataBean)o1).getEmployeeDepartment();
    			String strObj2 = ((EmployeeDataBean)o2).getEmployeeDepartment();
    			return strObj1.toUpperCase().compareTo(strObj2.toUpperCase());
    		 }
    	}
    call by

    Java Code:
        Collections.sort(empModels, new DesignationComparator());
        Collections.sort(empModels, new DepartmentComparator ());
    Please let me know how to achieve with better approach and I am currently using JDK1.4.

    Thanks,
    Chinnu

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why are you using such an outdated JDK? That approach looks fine though.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    That is a single sort though.
    In SQL terms, ORDER BY department, designation.
    In Java it would be a single comparator.
    Pseudo:
    Java Code:
    If thisDepartment == thatDepartment then
        if thisDesignation < thatDesignation then return -1
        else if thisDesignation > thatDesingation then return 1
        else return 0
    else if thisDepartment < thatDepartment then return -1
    else return 1
    I might have my 1's and -1's back to front, but tha's the idea.
    Running two Comparators across a single collection like you are trying smacks as unecessary work.

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I wanted to sort the ArrayList twice
    Why?

    I showed you how to do it in one step which is more efficient than doing it twice.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Chinnu55 View Post
    Java Code:
        Collections.sort(empModels, new DesignationComparator());
        Collections.sort(empModels, new DepartmentComparator ());
    That is never going to work; take this sequence for example: (B, 1) (A, 4) (B, 3) (A, 2). After you've sorted this on the leters the sequence may be (A, 4) (A, 2) (B, 1) (B, 3) and after you sort on the numbers the sequence can be (B, 1) (A, 2) (B, 3) (A, 4). You don't want that; you need to define a major and minor sort order and it was demonstrated by Camickr and Tolls; read their replies again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 4
    Last Post: 07-19-2011, 01:30 PM
  2. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 11:48 AM
  3. Arraylist errors with private fields
    By DJRome in forum New To Java
    Replies: 5
    Last Post: 03-02-2010, 12:00 PM
  4. Sorting an ArrayList
    By flesh-bound-book in forum New To Java
    Replies: 3
    Last Post: 02-13-2010, 12:20 PM
  5. Sorting/Searching Objects with multiple types.
    By gcampton in forum New To Java
    Replies: 20
    Last Post: 10-21-2009, 11:58 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
  •