我在这里拼凑了一个SSCCE:
房子.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);
}
}
排序测试.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
}
}
所以最终我想创建一个SetSorter
类,我可以在其中调用一个方法,该方法将返回以特定格式排序的 Set。
如果您不想阅读代码中的注释,我想根据以下内容进行排序:
- 状态.可用
- 状态.SOLD
- 状态.条件
- 城市(按字母顺序)
- 城市(按字母顺序倒序)
- 日期(上市最少天数)
- 日期(市场上的大多数日子)
我已经对其进行了一些阅读,看起来人们建议将其放入 TreeSet 以进行排序和使用比较器。我见过多个例子,人们要么为比较器创建一个单独的类,要么使指定的类实现可比较。
我还没有看到有人写了一个额外的类来处理所有的排序。这可能吗?如果是这样,有人可以告诉我从哪里开始吗?这些似乎比典型的整数比较稍微复杂一些。
编辑澄清
按Status.AVAILABLE排序时,我希望 Set 让对象按以下方式显示:
- Status.AVAILABLE(在顶部/第一)
- Status.CONTINGENT(在 Status.AVAILABLE/秒之后)
- Status.SOLD(在 Status.CONTINGENT/last 之后)
当按Status.CONTINGENT排序时,我希望集合排序如下:
- 状态.CONTINGENT
- 状态.SOLD
- 状态.可用
按Status.SOLD排序时,我希望按如下方式排序:
- 状态.SOLD
- 状态.CONTINGENT
- 状态.可用
编辑#2 终极目标:
我想有一个类,我可以简单地调用方法来对集合进行排序。
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
编辑#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());
我认为这将是一种简单的方法,但这些都是小类并且(在我看来)看起来很乱。谁想在 .java 中包含 7 个迷你类?
有人可以提供一些替代示例供我查看吗?