简体   繁体   English

在没有比较器的情况下使用Array.sort()对数组进行排序

[英]Sorting an array using Array.sort() without a comparator

I have to sort an Array of custom objects by one of the object's params using Arrays.sort() , but WITHOUT passing a Comparator. 我必须使用Arrays.sort()通过对象的参数之一对自定义对象的数组进行排序,但无需传递Comparator。 This is a homework assignment and my professor wants us to do it without a comparator. 这是一项家庭作业,我的教授希望我们在没有比较器的情况下完成作业。

To be more specific I have an array[] of object type 'Female', female is made up of params 'name' and 'age'. 更具体地说,我有一个对象类型为“女性”的array [],女性由参数“名称”和“年龄”组成。 I have to sort the array by age using Arrays.sort(femaleList) , but again I cannot use a Comparator. 我必须使用Arrays.sort(femaleList)按年龄对数组进行Arrays.sort(femaleList) ,但同样不能使用Comparator。

I'm trying to use .getAge() or something like that, but it's not working. 我正在尝试使用.getAge()或类似的东西,但是它不起作用。 I'm assuming there's some relatively simple solution that I'm overlooking, any help would be greatly appreciated. 我假设有一个我忽略的相对简单的解决方案,任何帮助将不胜感激。

Unless I'm missing something, implement Comparable<Female> like 除非我缺少任何东西,否则像这样实现Comparable<Female>

class Female implements Comparable<Female> {
    // ...
    public int compareTo(Female that) {
        if (this.age < that.age) {
            return -1;
        } else if (this.age > that.age) {
            return 1;
        }
        return 0;
    }
}

If you make the last line 如果您最后一行

return this.name.compareTo(that.name);

it will sort by name if any are the same age. 如果年龄相同,它将按name排序。

It can be as simple as this: 可以这样简单:

class Female implements Comparable<Female> {
    // ...
    public int compareTo(Female that) {
        return this.age - that.age;
    }
}

Because all you have to return is: 因为您只需要返回:

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. 负整数,零或正整数,因为此对象小于,等于或大于指定的对象。

http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

Be aware that this implementation comes with risks. 请注意,此实现存在风险。 This assumes that subtracting ages will not cause an overflow which could happen if their signs differ (essentially making this addition) and the sum of their magnitude was greater than age's type can hold. 假设减去年龄不会导致溢出,如果它们的符号不同(本质上是加法)并且其总和大于年龄类型可以容纳的范围,则不会发生溢出。

Another important point to note is don't use subtraction for comparing integral values because result of subtraction can overflow as every int operation in Java is modulo 2^32. 要注意的另一个重要点是不要使用减法来比较整数值,因为减法的结果可能会溢出,因为Java中的每个int运算都是模2 ^ 32。 use either Integer.compareTo() or logical operators for comparison. 使用Integer.compareTo()或逻辑运算符进行比较。 There is one scenario where you can use subtraction to reduce clutter and improve performance. 在一种情况下,您可以使用减法来减少混乱并提高性能。 As we know compareTo doesn't care magnitude, it just care whether result is positive or negative. 我们知道compareTo不在乎幅度,它只是在乎结果是正数还是负数。 While comparing two integral fields you can use subtraction if you are absolutely sure that both operands are positive integer or more precisely [their difference] must be less than Integer.MAX_VALUE. 在比较两个整数字段时,如果您绝对确定两个操作数都是正整数或更准确地说[它们的差]必须小于Integer.MAX_VALUE,则可以使用减法。 In this case there will be no overflow and your compareTo will be concise and faster. 在这种情况下,将不会有溢出,并且您的compareTo将变得简明快捷。

http://javarevisited.blogspot.com/2011/11/how-to-override-compareto-method-in.html http://javarevisited.blogspot.com/2011/11/how-to-override-compareto-method-in.html

您需要使Female工具Comparable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM