Link to home
Start Free TrialLog in
Avatar of heyday2004
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.
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of heyday2004
heyday2004

ASKER

The field Date. Thanks!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.getFileDate());
    }
});

    return filesDb;

  }

Thanks a lot.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the excellent replies, I really appreciate it.
Thanks!