java 对包含自定义类的列表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10396970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort a list that contains a custom class
提问by zaakun
so I'm currently doing an exercise for college that has several optional parts (because we havn't done this in class yet), one of them being to use lists instead of arrays (so it'd be variable size) and another one printing the list sorted by points (I'll get to that now)
所以我目前正在为大学做一个练习,它有几个可选部分(因为我们还没有在课堂上做过),其中一个是使用列表而不是数组(所以它的大小是可变的)和另一个打印按点排序的列表(我现在就开始)
So, I have the Player.java class which looks like this.
所以,我有看起来像这样的 Player.java 类。
public class Player {
String name;
String password;
int chips;
int points;
public Player (String n, String pw, int c, int p) {
name = n;
password = pw;
chips = c;
points = p;
}
public String getName () {
return name;
}
public void setName (String n) {
name = n;
}
public void setPW (String pw) {
password = pw;
}
public String getPW () {
return password;
}
public void setChips (int c) {
chips = c;
}
public int getChips () {
return chips;
}
public void setPoints (int p) {
points = p;
}
public int getPoints () {
return points;
}
}
}
Pretty simple, then I'm creating a List with this (in another class):
很简单,然后我用这个(在另一个类中)创建一个列表:
List<Player> lplayer = new ArrayList<Player>();
Adding players with this:
用这个添加玩家:
lplayer.add(new Player(n,pw,c,p))`
And finally reading their stats with this:
最后用这个阅读他们的统计数据:
public int search_Player (String n) {
String name;
int i = 0;
boolean found = false;
while ((i <= tp) && (!found)) {
name = lplayer.get(i).getName();
if (name.equals(n)) {
found = true;
}
i++;
}
return (found == true) ? i-1 : -1;
}
public Player show_Player (int i) {
return lplayer.get(i);
}
public void list_Players() {
Collections.sort(lplayer);
int i2;
if (tp > 0) { // variable which contains number of total players
for (int i = 0;i<tp;i++) {
i2 = i+1;
System.out.println ("\n"+i2+". "+lplayer.get(i).getName()+" [CHIPS: "+lplayer.get(i).getChips()+" - POINTS: "+lplayer.get(i).getPoints()+"]");
}
}
else {
System.out.println ("There are no players yet.");
}
}
So that's basically all the code. As you can see the I already have a list_Players function but that just prints it in the order it was added. I need a way to print in sorted by the points each player has (so basically a ranking).
这就是基本上所有的代码。正如你所看到的,我已经有一个 list_Players 函数,但它只是按照添加的顺序打印它。我需要一种方法来按每个玩家的积分排序(所以基本上是排名)。
As you can see I'm pretty new to java so please try not to come up with a very complicated way of doing it.
正如你所看到的,我对 Java 很陌生,所以请尽量不要想出一个非常复杂的方法。
I've already searched for it and found things like Collections.sort(list) but I guess that's not what I need right here.
我已经搜索过它并找到了 Collections.sort(list) 之类的东西,但我想这不是我在这里需要的。
Thank you!
谢谢!
回答by Alexander Pavlov
You can use the public static <T> void sort(List<T> list, Comparator<? super T> c)
overload in Collections
- provide the comparator you need (can be just an anonymous class) - and you are all set!
您可以使用public static <T> void sort(List<T> list, Comparator<? super T> c)
重载Collections
- 提供您需要的比较器(可以只是一个匿名类) - 一切就绪!
EDIT: Thisdescribes how the method works. In brief, you'll implement your call as
编辑: 这描述了该方法的工作原理。简而言之,您将调用实现为
Collections.sort(list, new Comparator<Player>() {
int compare(Player left, Player right) {
return left.getPoints() - right.getPoints(); // The order depends on the direction of sorting.
}
});
That's it!
而已!
回答by Thomas Uhrig
Collections.sort(list)
could definitely by a solution for your problem. It's a way to sort your collections provided by Java. If you are writing a "real world" application (not an exercise for collage) this would be the way you doing it.
Collections.sort(list)
绝对可以解决您的问题。这是一种对 Java 提供的集合进行排序的方法。如果您正在编写“真实世界”应用程序(不是拼贴练习),这将是您的方式。
To let Collections.sort(list)
works, you have to implement an interface call Comparaple. By implementing this interface, the sort will know how to order your elements.
为了让它Collections.sort(list)
工作,你必须实现一个接口调用Comparaple。通过实现这个接口,排序将知道如何对您的元素进行排序。
But because it's a exercise for collage, this is perhaps a little bit to easy. If you want (or must) implement you own sorting algorithm, try first to sort a common list of numbers (1, 5, 2, 7...). You can extend such an sorting algorithm easily for your own classes.
但是因为它是拼贴练习,所以这可能有点容易。如果您想(或必须)实现自己的排序算法,请先尝试对常见的数字列表(1、5、2、7...)进行排序。您可以为自己的类轻松扩展这样的排序算法。