【发布时间】:2025-11-29 21:50:02
【问题描述】:
我想按照我想要的方式对 ArrayList 对象进行排序。这是我的目标:
public class PaintRegion extends JPanel {
public ArrayList<Region> m_region = new ArrayList<Region>();
// some codes
}
及其类:
public class Region {
private String m_region;
private Integer m_totalCount;
private double m_normalizedCount;
public String getRegion() {
return m_region;
}
public void setRegion(String region) {
this.m_region=region;
}
public Integer getTotalCount() {
return m_totalCount;
}
public void setTotalCount(Integer totalCount) {
this.m_totalCount = totalCount;
}
public double getNormalizedCount() {
return m_normalizedCount;
}
public void setNormalizedCount(double normalizedCount) {
this.m_normalizedCount = normalizedCount;
}
public boolean Print_Region() {
System.out.println("State::Print_Region(): " +
this.getRegion()+ ", "+
this.getTotalCount()+ ", "+
this.getNormalizedCount());
return true;
}
}
我有 5 个区域名称,它们是 {"Southeast","Southwest","Northeast","West","Midwest"}
我想按“West”、“Midwest”、“Southeast”、“Southwest”、“Northeast”对这个对象进行排序
如何排序?
【问题讨论】:
-
通过覆盖
equals()和toString()方法 -
你重新排列名字的逻辑是什么?
-
@Satish 这些方法不用于排序。你需要实现一个
Comparator<Region>。 -
谷歌搜索“如何在 Java 中对对象进行排序”可能会返回数百个相关结果。阅读并尝试一下。
-
@SatishPatel 你能详细说明你刚才所说的吗