Reputation: 926
I've thrown together a SSCCE here:
House.java :
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class House {
private Status currentStatus;
private String city;
private Date date;
public enum Status { AVAILABLE,
SOLD,
CONTINGENT
}
public House(Status s, String c, String d) throws ParseException {
currentStatus = s;
city = c;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(d);
}
}
SortingTest.java :
import java.text.ParseException;
import java.util.HashSet;
import sortingtest.House.Status;
public class SortingTest {
public static void main(String[] args) throws ParseException {
HashSet<House> houses = new HashSet<House>();
houses.add(new House(Status.AVAILABLE, "New York City", "2007-11-11"));
houses.add(new House(Status.SOLD, "Los Angeles", "2005-06-11"));
houses.add(new House(Status.AVAILABLE, "Chicago", "2012-05-03"));
houses.add(new House(Status.CONTINGENT, "Portland", "2007-10-11"));
//Sort HashSet of House objects by criteria listed below
//sort by Status.AVAILABLE
//call sort
//System.out.println("Sorted by available");
//iterate set and print out sorted houses
//sort by Status.SOLD
//call sort
//System.out.println("Sorted by sold");
//iterate set and print out sorted houses
//sort by Status.CONTINGENT
//call sort
//System.out.println("Sorted by contingent");
//iterate set and print out sorted houses
//sort by City
//call sort
//System.out.println("Sorted alphabetically by City");
//iterate set and print out sorted houses
//sort by City
//call sort
//System.out.println("Sorted reverse alphabetically by City");
//iterate set and print out sorted houses
//sort by Date (newest)
//call sort
//System.out.println("Sorted by newest date (fewest days on market)");
//iterate set and print out sorted houses
//sort by Date (oldest)
//call sort
//System.out.println("Sorted oldest date (most days on market)");
//iterate set and print out sorted houses
}
}
So ultimately I'm wanting to create a SetSorter
class where I can just call a method that will return the Set sorted in a particular format.
In case you didn't want to read the comments in the code, I'm wanting to sort based on:
I've done a bit of reading on it, and it looks like people suggest making it into a TreeSet for sorting and using a comparator. I've seen multiple examples where people either create a separate class just for the comparator or they make the specified class implement comparable.
What I haven't seen is someone write an extra class to handle all of the sorting. Is this possible? If so, can someone show me where to start? These seems like slightly more complicated comparisons than typical integer comparisons.
Edit for clarification
When sorting by Status.AVAILABLE I would like the Set to have the objects appear by:
When sorting by Status.CONTINGENT I want the set sorted as follows:
When sorting by Status.SOLD I want the set sorted as follows:
Edit #2 Ultimate Goal:
I would like to have a class where I can simply call methods to sort the set.
Ie:
//sort by date
SetSorter.sortByData(treeSet); //returns TreeSet sorted by date
//sort by city
SetSorter.sortByCity(treeSet); //returns TreeSet sorted by City
//sort by other criteria
Edit #3
class SortByCity implements Comparator<House> {
@Override
public int compare(House h1, House h2) {
return h1.getCity().compareTo(h1.getCity());
}
}
houses = new TreeSet(new SortByCity());
I think this would be a simple way of doing this, but these would all be little classes and (in my opinion) look messy. Who wants to have 7 mini classes inside of a .java?
Can someone provide some alternative examples for me to look at?
Upvotes: 0
Views: 7971
Reputation: 93968
Here are some samples on sorting. I haven't performed the date or reverse alphabet sorting (that's an assignment). Note the inline comment about comparing two houses to value 0
!
public class HouseSorter {
enum Status {
SOLD, AVAILABLE, CONTINGENT;
}
/**
* Immutable house (if a house is sold or not does not change a house, use a
* Map instead).
*/
private static class House {
private final String city;
House(String city) {
this.city = city;
}
public String getCity() {
return city;
}
@Override
public String toString() {
return "House in " + city;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!House.class.isAssignableFrom(obj.getClass())) {
return false;
}
return this.city.equalsIgnoreCase(((House) obj).city);
}
@Override
public int hashCode() {
return city.hashCode();
}
}
public static SortedSet<House> sortAlphabetically(Set<House> houses) {
TreeSet<House> sortedHouses = new TreeSet<House>(
new Comparator<House>() {
@Override
public int compare(House o1, House o2) {
return o1.getCity().compareTo(o2.getCity());
}
});
sortedHouses.addAll(houses);
return sortedHouses;
}
public static SortedSet<House> sortByStatus(
final Map<House, Status> houseStatusMap) {
TreeSet<House> sortedHouses = new TreeSet<House>(
new Comparator<House>() {
@Override
public int compare(House o1, House o2) {
int compareByStatus = houseStatusMap.get(o1).compareTo(
houseStatusMap.get(o2));
if (compareByStatus != 0) {
return compareByStatus;
}
// you need an additional compare, until none of the
// houses compare with result 0
// otherwise the houses would be equal and therefore
// removed from the set
return o1.getCity().compareTo(o2.getCity());
}
});
sortedHouses.addAll(houseStatusMap.keySet());
return sortedHouses;
}
/**
* @param args
*/
public static void main(String[] args) {
final Map<House, Status> houseStatusMap = new HashMap<House, Status>();
House house0 = new House("Beverwijk");
houseStatusMap.put(house0, Status.SOLD);
House house1 = new House("Opmeer");
houseStatusMap.put(house1, Status.SOLD);
House house2 = new House("Amstelveen");
houseStatusMap.put(house2, Status.AVAILABLE);
House house3 = new House("Haarlem");
houseStatusMap.put(house3, Status.CONTINGENT);
System.out.println(sortAlphabetically(houseStatusMap.keySet()));
System.out.println(sortByStatus(houseStatusMap));
}
}
Upvotes: 1