This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes sorting a collection of User Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "sorting a collection of User Objects" Watch "sorting a collection of User Objects" New topic
Author

sorting a collection of User Objects

Rod McLure
Greenhorn

Joined: Mar 04, 2010
Posts: 24
I have a list of objects called CustomerLots, I want to sort
using Collections.sort(list, comparator) maybe?
I want to sort by (Garden)Id, and then lotName (alphabetic)

public class CustomerLots {
private CustomerLotsId id;

private Lot lot;

private Customer customer;

private CustomerType customerType;
}
public class Lot {

// Fields

private Integer id;
private Garden garden;
private LotType lotType;
private String lotName;
private String lotNameNumber;
private String comments;
private String status;
private String active;
private Integer lotMax;
private Date createDate;
private String createUser;
private Date lastUpdDate;
private String lastUpdUser;
private Set customerLotses = new HashSet(0);
}
public class Garden{

private Integer id;
private String gardenName;
private String active;
private Date createDate;
private String createUser;
private Date lastUpdDate;
private String lastUpdUser;
private SortedSet<Lot> lots = new TreeSet<Lot>();
}
any help appreciated, (this is using hibernate, but I guess that doesn't matter)
Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Please Use Code Tags. Also, use our search. The question about comparing on multiple fields has been asked and answered a couple of times before.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Agree with Rob. Also, any class with that many fields looks like iffy design, and might better be subdivided into several classes.
Rod McLure
Greenhorn

Joined: Mar 04, 2010
Posts: 24
wow! quick responses, my apologies, I am a greenhorn.

I have searched but all the examples I have found
are like sort Employee by Last name first name etc

my issue is with the nesting? number of levels? or whatever it is called
I want to sort customer Lot by Garden id (or name) but
CustomerLots does not have a garden name
it has a lot
lot does not have a garden name it has a garden
finally garden has an id (and a name)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
I suspect you are actually sorting by first name, then last name. do it in reverse order; because most of the sorting algorithms used in Java (eg merge sort) are "stable" you can sort on "Campbell" then last name, so "Campbell Ritchie" and "Campbell Roberts" will come out in the correct order. Sorting on garden lots is similar, but you will have to use Campbell's favourite technology, as described here ( ), and write down how you are going to "order" the garden lots.
Rod McLure
Greenhorn

Joined: Mar 04, 2010
Posts: 24
seems to be working, Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
 
GeeCON Prague 2014
 
subject: sorting a collection of User Objects