compareTo 方法如何与 array.sort 一起对数组进行排序?
How can the compareTo method work together with array.sort to sort an array?
我知道 compareTo 可以 return 正负值和零值。
所以如果我使用
public class x implements Comparable <Object O> {/*...*/}
并在里面赋值
public int compareTo(int i) {
if (this.intg < i) { return -1; }
else if (this.intg > i) { return 1; }
else { return 0; }
}
我可以这样使用排序 Array.sort(O[]objects)
吗?
我不知道array.sort()
和compareTo()
的联系。
它甚至不调用比较方法对其进行排序。
那么 compareTo()
输入的真正作用是什么?我用array.sort()
的时候调用不了这个方法可以传到哪里
这是 Arrays.sort(Object[])
的 Javadoc:
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2)
must not throw a ClassCastException
for any elements e1
and e2
in the array).
所以sort方法确实依赖数组元素的Comparable
接口
您可以使用 Collections.sort
方法对对象进行 List
就地排序。第二个参数是您的 Comparator
实现。
示例:
List<Animal> animals = .. some ArrayList for example;
Comparator<Animal> comparator = new Comparator<Animal>() {
public int compare(Animal d1, Animal d2) {
int v1 = d1.intg;
int v2 = d2.intg;
return (v1 > v2 ? 1 : (v1 == v2 ? 0 : -1));
}
};
Collections.sort(animals, comparator);
// the 'animals' collection is now sorted based on the `intg` property
通常你最好创建一个 Comparator 而不是实现 Comparable。然后您可以将其传递给集合排序方法:
Collections.sort(myArrayList, new Comparator<MyObject>(){
public int compare(MyObject o1, MyObject o2){
return Integer.compare(o1.value == o2.value);
}
});
编辑:
在阅读了一些 OP 评论后,我们可以证明确实调用了 compareTo 方法:
public class TestClass implements Comparable<TestClass> {
private int value;
public int getValue(){
return this.value;
}
public void setValue(int v){
this.value = v;
}
@Override
public int compareTo(TestClass arg) {
System.out.println("In compareTo");
return Integer.compare(this.value, arg.value);
}
}
如果我们执行以下操作:
ArrayList<TestClass> a = new ArrayList<>();
for( int i = 0; i < 10; i++ ){
TestClass t = new TestClass();
t.setValue(i);
a.add(t);
}
System.out.println("Before sort.");
Collections.sort(a);
我们将看到从排序方法中调用了 compareTo 方法,因为终端将显示我们的跟踪语句:
Before sort.
In compareTo
In compareTo
In compareTo
In compareTo
In compareTo
In compareTo
In compareTo
In compareTo
In compareTo
我知道 compareTo 可以 return 正负值和零值。
所以如果我使用
public class x implements Comparable <Object O> {/*...*/}
并在里面赋值
public int compareTo(int i) {
if (this.intg < i) { return -1; }
else if (this.intg > i) { return 1; }
else { return 0; }
}
我可以这样使用排序 Array.sort(O[]objects)
吗?
我不知道array.sort()
和compareTo()
的联系。
它甚至不调用比较方法对其进行排序。
那么 compareTo()
输入的真正作用是什么?我用array.sort()
这是 Arrays.sort(Object[])
的 Javadoc:
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is,
e1.compareTo(e2)
must not throw aClassCastException
for any elementse1
ande2
in the array).
所以sort方法确实依赖数组元素的Comparable
接口
您可以使用 Collections.sort
方法对对象进行 List
就地排序。第二个参数是您的 Comparator
实现。
示例:
List<Animal> animals = .. some ArrayList for example;
Comparator<Animal> comparator = new Comparator<Animal>() {
public int compare(Animal d1, Animal d2) {
int v1 = d1.intg;
int v2 = d2.intg;
return (v1 > v2 ? 1 : (v1 == v2 ? 0 : -1));
}
};
Collections.sort(animals, comparator);
// the 'animals' collection is now sorted based on the `intg` property
通常你最好创建一个 Comparator 而不是实现 Comparable。然后您可以将其传递给集合排序方法:
Collections.sort(myArrayList, new Comparator<MyObject>(){
public int compare(MyObject o1, MyObject o2){
return Integer.compare(o1.value == o2.value);
}
});
编辑:
在阅读了一些 OP 评论后,我们可以证明确实调用了 compareTo 方法:
public class TestClass implements Comparable<TestClass> {
private int value;
public int getValue(){
return this.value;
}
public void setValue(int v){
this.value = v;
}
@Override
public int compareTo(TestClass arg) {
System.out.println("In compareTo");
return Integer.compare(this.value, arg.value);
}
}
如果我们执行以下操作:
ArrayList<TestClass> a = new ArrayList<>();
for( int i = 0; i < 10; i++ ){
TestClass t = new TestClass();
t.setValue(i);
a.add(t);
}
System.out.println("Before sort.");
Collections.sort(a);
我们将看到从排序方法中调用了 compareTo 方法,因为终端将显示我们的跟踪语句:
Before sort.
In compareTo
In compareTo
In compareTo
In compareTo
In compareTo
In compareTo
In compareTo
In compareTo
In compareTo