Results 1 to 9 of 9
- 08-12-2011, 09:40 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 20
- Rep Power
- 0
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
- 08-12-2011, 09:46 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
- 08-12-2011, 09:48 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You want to write comparators for you Employee class, and pass it into the Collections.sort method.
Comparator (Java Platform SE 6)
- 08-13-2011, 11:19 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
That's actually sorting by department then designation then name.
- 08-13-2011, 01:00 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 20
- Rep Power
- 0
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
call byJava 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()); } }
Please let me know how to achieve with better approach and I am currently using JDK1.4.Java Code:Collections.sort(empModels, new DesignationComparator()); Collections.sort(empModels, new DepartmentComparator ());
Thanks,
Chinnu
- 08-13-2011, 01:08 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Why are you using such an outdated JDK? That approach looks fine though.
- 08-13-2011, 01:18 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
That is a single sort though.
In SQL terms, ORDER BY department, designation.
In Java it would be a single comparator.
Pseudo:
I might have my 1's and -1's back to front, but tha's the idea.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
Running two Comparators across a single collection like you are trying smacks as unecessary work.
- 08-13-2011, 04:31 PM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Why?I wanted to sort the ArrayList twice
I showed you how to do it in one step which is more efficient than doing it twice.
- 08-13-2011, 06:02 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Initializing objects- nonstatic fields cannot be referenced from a static context
By Nazneen Ali in forum New To JavaReplies: 4Last Post: 07-19-2011, 01:30 PM -
sorting arraylist based on another arraylist
By busdude in forum New To JavaReplies: 4Last Post: 02-07-2011, 11:48 AM -
Arraylist errors with private fields
By DJRome in forum New To JavaReplies: 5Last Post: 03-02-2010, 12:00 PM -
Sorting an ArrayList
By flesh-bound-book in forum New To JavaReplies: 3Last Post: 02-13-2010, 12:20 PM -
Sorting/Searching Objects with multiple types.
By gcampton in forum New To JavaReplies: 20Last Post: 10-21-2009, 11:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks