英文:
Java - Implement compareTo() ArrayList custom objects
问题
我正在实现并在我的类“History”中使用Comparable接口。这样,我想对一个包含History对象的ArrayList
该类的构造函数如下:
public History(long id, String res, double deposit, double odds, String sport, double ret, String user_name, Date date) {
this.id = id;
this.res = res;
this.deposit = deposit;
this.odds = odds;
this.sport = sport;
this.ret = ret;
this.user_name = user_name;
this.date = date;
}
@Override
public int compareTo(History o) {
return this.getDepInt().compareTo(o.getDepInt());
}
此外,我有相应的方法来返回变量,例如“getDepInt()”,“getOddsInt()”等。
这可以正常工作,我能够按存款变量对列表进行排序:
[172 Loss | 1000.0 | 1.1 | 1100.0 | F
, 170 Loss | 900.0 | 2.75 | 2475.0 | F
, 168 Won | 300.0 | 2.75 | 825.0 | F
, 169 Won | 200.0 | 2.75 | 550.0 | F
, 105 Loss | 175.0 | 2.75 | 481.25 | F
, 167 Won | 100.0 | 2.5 | 250.0 | F
, 166 Loss | 100.0 | 2.5 | 250.0 | F
, 165 Won | 100.0 | 2.5 | 250.0 | F
, 164 Won | 100.0 | 2.5 | 250.0 | F
, 171 Loss | 20.0 | 1.5 | 30.0 | F
]
但是,我希望能够按所有变量对列表进行排序,以便用户可以选择按“赢/输”等方式对列表进行排序。
我的问题是compareTo(History o)方法会被覆盖,而我实际上没有在我的代码中调用该方法。是否有解决这个问题的方法?
英文:
I'm implementing and using the Comparable interface in my class "History". This way I want to sort an ArrayList <History> of History objects.
The class constructor looks like this:
public History(long id, String res, double deposit, double odds, String sport, double ret, String user_name, Date date) {
this.id = id;
this.res = res;
this.deposit = deposit;
this.odds = odds;
this.sport = sport;
this.ret = ret;
this.user_name = user_name;
this.date = date;
}
@Override
public int compareTo(History o) {
return this.getDepInt().compareTo(o.getDepInt());
}
Also, I have respective methods to return the variables e.g. "getDepInt()", getOddsInt() etc.
This works fine, and im able to sort the list by the deposit variable:
[172 Loss | 1000.0 | 1.1 | 1100.0 | F
, 170 Loss | 900.0 | 2.75 | 2475.0 | F
, 168 Won | 300.0 | 2.75 | 825.0 | F
, 169 Won | 200.0 | 2.75 | 550.0 | F
, 105 Loss | 175.0 | 2.75 | 481.25 | F
, 167 Won | 100.0 | 2.5 | 250.0 | F
, 166 Loss | 100.0 | 2.5 | 250.0 | F
, 165 Won | 100.0 | 2.5 | 250.0 | F
, 164 Won | 100.0 | 2.5 | 250.0 | F
, 171 Loss | 20.0 | 1.5 | 30.0 | F
]
However, I want to be able to sort the list on all varibles, so the user can select which way the they want to sort the list e.g. sort the list by "Won/Loss".
My problem is that the compareTo(History o) method overwrites, and im not actually calling the method in my code. Is there a way of solving this?
答案1
得分: 3
这正是Comparator
接口的用途。因为Comparable
接口的compareTo()
方法只用于表示该类型对象的“自然”顺序(按照最常见的特征排序)。
要实现按照任意特征进行比较,可以编写一些实现Comparator
接口的类,例如...
public class HistoryOddsComparator implements Comparator<History> {
@Override
public int compare(History o1, History o2) {
return o1.getOddsInt() - o2.getOddsInt();
}
}
...这样您可以调用...
Collections.sort(historyList, new HistoryOddsComparator());
(这只是一个示例!)
英文:
This is exactly what the Comparator
-Interface is for. Because the compareTo()
method of the Comparable
interface is only there to represent the "natural" order of objects of this type (order by the most common feature).
To implement comparison by arbitrary features write a handful of classes implementing Comparator
, such as ...
public class HistoryOddsComparator implements Comparator<History> {
@Override
public int compare(History o1, History o2) {
return o1.getOddsInt() - o2.getOddsInt();
}
}
... so you can call ...
Collections.sort(historyList, new HistoryOddsComparator());
(just an example!)
答案2
得分: 1
你应该使用Comparator。
示例在这里。
只需为想要按每个字段排序的字段实现一个比较器。
英文:
You should use Comparator.
The example is here.
Just implement one comparator for every field you want to sort by.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论