heyday2004
asked on
How to sort a set of objects based on a certain field?
How to sort below set of objects, so that the returned filesDb set will have records sorted by fileDate field in this object?
In SampleFile object, it has these fields:
fileDate and fileId
file date is like:
2011-05-11
2011-05-10
private Set<SampleFile> getFiles(Set<SampleFile> files) {
Set<SampleFile> filesDb = new HashSet();
for (SampleFile f : files) {
filesDb.add(fileDao.get(f. getFileId( )));
}
return filesDb;
}
Thanks.
In SampleFile object, it has these fields:
fileDate and fileId
file date is like:
2011-05-11
2011-05-10
private Set<SampleFile> getFiles(Set<SampleFile> files) {
Set<SampleFile> filesDb = new HashSet();
for (SampleFile f : files) {
filesDb.add(fileDao.get(f.
}
return filesDb;
}
Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all the replies. what I need is to sort Set, not List.
Listed below is what I got from the replies, will it return a sorted Set (filesDb) by FileDate (that's all my purpose)? Any further suggestion to get a sorted set of filesDb (by FileDate)?
private Set<SampleFile> getFiles(Set<SampleFile> files) {
Set<SampleFile> filesDb = new HashSet();
for (SampleFile f : files) {
filesDb.add(fileDao.get(f. getFileId( )));
}
Collections.sort(filesDb, new Comparator<SampleFile>() {
public int compare(SampleFile s1, SampleFile s2) {
return s1.getFileDate().compareTo (s2.getFil eDate());
}
});
return filesDb;
}
Thanks a lot.
Listed below is what I got from the replies, will it return a sorted Set (filesDb) by FileDate (that's all my purpose)? Any further suggestion to get a sorted set of filesDb (by FileDate)?
private Set<SampleFile> getFiles(Set<SampleFile> files) {
Set<SampleFile> filesDb = new HashSet();
for (SampleFile f : files) {
filesDb.add(fileDao.get(f.
}
Collections.sort(filesDb, new Comparator<SampleFile>() {
public int compare(SampleFile s1, SampleFile s2) {
return s1.getFileDate().compareTo
}
});
return filesDb;
}
Thanks a lot.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry for my late replies because I was out of town. One last question:
For CEHJ's solution:
If my original set has:
record A
record b
and if both records have exactly the same FileDate
Then seems in the final sortedSet, only record A will be added in the sortedSet (since it provides the same compare result).
Please advise if this is true when we used SortedSet (based on the running result, it seems to be true). Thanks!
For CEHJ's solution:
If my original set has:
record A
record b
and if both records have exactly the same FileDate
Then seems in the final sortedSet, only record A will be added in the sortedSet (since it provides the same compare result).
Please advise if this is true when we used SortedSet (based on the running result, it seems to be true). Thanks!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the replies. I'm still confused since in CEHJ's solution, the input Set: files have object A and B. And my output of filesDb (sorted Set) seems only contain one object (A or B). The filesDb is a new sorted Set, with record added like this:
for (SampleFile f : files) {
filesDb.addAll(file....);
}
return filesDb;
Any possibility that in this case, the returned filesDb might just contain record A or B but not both?
Thanks!
for (SampleFile f : files) {
filesDb.addAll(file....);
}
return filesDb;
Any possibility that in this case, the returned filesDb might just contain record A or B but not both?
Thanks!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks. This works. So for sorted set, if the compare method returns 0, then even the hash code is different, the two records will be considered duplicate? Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the excellent replies, I really appreciate it.
ASKER
Thanks!
ASKER