java 排序类数组java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17336174/
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
sorting class array java
提问by navalp3
I have made a class as follows:
我做了一个类如下:
class strVal{
double val;
String str;
}
Now I made an array of this class now I want to sort that array by strVal.val
. I want to know if there is any standard defined method in Java?
现在我制作了一个此类的数组,现在我想对该数组进行排序strVal.val
。我想知道Java中是否有任何标准定义的方法?
回答by Gauthier JACQUES
Implement java.lang.Comparable interface, and use java.util.Arrays.sort(...) to sort an array of StrVal:
实现java.lang.Comparable接口,使用java.util.Arrays.sort(...)对StrVal数组进行排序:
public class StrVal implements Comparable<StrVal> {
private double val;
private String str;
public StrVal(double val, String str) {
this.val = val;
this.str = str;
}
@Override
public int compareTo(StrVal o) {
return (int)(this.val - o.val);
}
public double getVal() {
return val;
}
public String getStr() {
return str;
}
}
To sort the array :
对数组进行排序:
StrVal[] array;
...
Arrays.sort(array)
回答by Juned Ahsan
You need to implement Comparatoror Comparableinterface and then call Collections.sorton the collection of obejects of your class strVal. Follow this tutorial for learning more about collections sorting:
您需要实现Comparator或Comparable接口,然后对类 strVal 的对象集合调用Collections.sort。按照本教程了解有关集合排序的更多信息:
http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
回答by Burkhard
Collections.sort might help you. Use a List<strVal>
and then:
Collections.sort 可能会帮助你。使用 aList<strVal>
然后:
Collections.sort(list, comparator);
Some more code:
还有一些代码:
List<strVal> list = ...;
Collections.sort(list, new Comparator<strVal>()
{
@Override
public int compare(strVal o1, strVal o2)
{
return o1.val - o2.val;
}
});
Or if you don't want to use a List, use Arrays.sort(array, comparator)
.
或者,如果您不想使用 List,请使用Arrays.sort(array, comparator)
.
回答by NINCOMPOOP
now I made an arrayof this class now I want to sort that array by strVal.val.
现在我制作了这个类的数组,现在我想通过 strVal.val 对该数组进行排序。
You can implement a Comparator, override its compare()method and use Arrays.sort().
您可以实现Comparator,覆盖其compare()方法并使用Arrays.sort()。
In case you use a List<strVal>
use Collections.sort().
如果您使用List<strVal>
使用Collections.sort()。
If you think that the comparison of the class objects can be done only by its val
property and that property defines the natural ordering of the objects then your class can implement Comparable.
如果您认为类对象的比较只能通过其val
属性完成,并且该属性定义了对象的自然顺序,那么您的类可以实现Comparable。
Comparator :
比较器:
new Comparator<strVal>()
{
@Override
public int compare(strVal o1, strVal o2)
{
return o1.val - o2.val;
}
});
Comparable:
可比:
class strVal implements Comparable<strVal>{
double val;
String str;
@Override
public int compareTo(strVal o) {
return this.val - o.val;
}
}
P.s.:Please adhere to Java naming conventions.
Ps:请遵守Java命名约定。
回答by JREN
You will have to implement the comparable interface for that and make your val field the field where the compare will take place.
您必须为此实现可比较的接口,并使您的 val 字段成为进行比较的字段。
More info here: http://www.javapractices.com/topic/TopicAction.do?Id=10
更多信息在这里:http: //www.javapractices.com/topic/TopicAction.do?Id=10
回答by wrm
you need to implement Comparable-interface and use the sort methods of the collection
您需要实现Comparable-interface 并使用集合的排序方法
回答by mariosangiorgio
You should use the sort
method defined in the Arrays
utility class.
In order to sort according to your key you should either make your class implement Comparable
or provide a Comparator
.
为了根据您的键进行排序,您应该使您的类实现Comparable
或提供Comparator
.