Skip to content

Instantly share code, notes, and snippets.

@spencerwi
Last active December 22, 2015 21:09
Show Gist options
  • Save spencerwi/6531144 to your computer and use it in GitHub Desktop.
Save spencerwi/6531144 to your computer and use it in GitHub Desktop.
for a blog post
List<PersonEntity> attendees = conference.getAttendees();
Collections.sort(new Comparator<PersonEntity> {
@Override
public int sort(PersonEntity person1, PersonEntity person2) {
final int BOTH_ARE_EQUAL = 0;
int comparisonByArrivalDateResult = this.compareByArrivalDate(person1, person2);
if (comparisonByArrivalDateResult == BOTH_ARE_EQUAL) {
int comparisonByTicketTierResult = this.compareByTicketTier(person1, person2);
if (comparisonByTicketTierResult == BOTH_ARE_EQUAL) {
return this.compareByLastName(person1, person2);
}
return comparisonByTicketTierResult
}
return comparisonByArrivalDateResult;
}
private int compareByArrivalDate(person1, person2) {
ArrivalDate person1ArrivalDate = person1.getArrivalDate(),
person2ArrivalDate = person2.getArrivalDate();
if (person1ArrivalDate == null) person1ArrivalDate = DEFAULT_ARRIVAL_DATE;
if (person2ArrivalDate == null) person2ArrivalDate = DEFAULT_ARRIVAL_DATE;
return person1ArrivalDate.compareTo(person2ArrivalDate);
}
private int compareByTicketTier(person1, person2) {
TicketTier person1TicketTier = person1.getTicketTier(),
person2TicketTier = person2.getTicketTier();
if (person1TicketTier == null) person1TicketTier = DEFAULT_TICKET_TIER;
if (person2TicketTier == null) person2TicketTier = DEFAULT_TICKET_TIER;
return person1TicketTier.compareTo(person2TicketTier);
}
private int compareByLastName(person1, person2) {
String person1LastName = person1.getLastName(),
person2LastName = person2.getLastName();
if (person1LastName == null) person1LastName = "";
if (person2LastName == null) person2LastName = "";
return person1LastName.compareTo(person2LastName);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment