dcsimg
Sorting on multiple conditions
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, November 7, 2007 04:02 AM

Hi all, I just want to know if I can sort the elements of a ConcurrentHashMap on multiple conditions. Suppose my ConcurrentHashMap has Student object with following properties: StudentID Date of Join etc... I am looking a method to sort it based on Date of join and Student ID(as I don't want to write my own sorting algorithm). If I was fetching it from a DB, effectively I would have used the following SQL query: SELECT STUDENT_ID, DOJ FROM STUDENT ORDER BY DOJ, STUDENT_ID. Unfortunately I do not obtain the data from DB. With Java, I want to know if there is any mechanism to sort them in the same order as Oracle would have sorted from the above query. I tried the following way: 1. S   More>>

Hi all,
I just want to know if I can sort the elements of a ConcurrentHashMap on multiple conditions.

Suppose my ConcurrentHashMap has Student object with following properties:

StudentID
Date of Join
etc...

I am looking a method to sort it based on Date of join and Student ID(as I don't want to write my own sorting algorithm). If I was fetching it from a DB, effectively I would have used the following SQL query: SELECT STUDENT_ID, DOJ FROM STUDENT ORDER BY DOJ, STUDENT_ID. Unfortunately I do not obtain the data from DB. With Java, I want to know if there is any mechanism to sort them in the same order as Oracle would have sorted from the above query.

I tried the following way:
1. Sorted based on DOJ by using Collections.sort(map, Comparator1 to compare DOJ),
2. Then sorted based StudentID using Collections.sort(map, Comparator2 to compare StudentID).

But after sorting in step2, it re-aligns the sorted items just by StudentID not as DOJ, StudentID.
I gues that is how it is supposed to sort, but just wanted to know if there is any way to sort by DOJ, StudentID.

Any help would be greatly appreciated.

Thanks,
Ajay

   <<Less

Re: Sorting on multiple conditions

Posted By:   Robert_Lybarger  
Posted On:   Wednesday, November 7, 2007 08:28 AM

Usually you need to write just one Comparator implementation that does all the checks you need. In your case, first compare the date members. If the comparison is non-zero, return that value. If that first comparison is zero, then compare the id members and return that value. (Or does this not sound like what you need? It's early for me yet...)
About | Sitemap | Contact