• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sorting a collection of User Objects

 
Rod McLure
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48377
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48377
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seems to be working, Thanks
 
Campbell Ritchie
Sheriff
Posts: 48377
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic