9
Groovy的飛船運營商<=>
提供了一種實現比較的簡單方法。我怎樣才能以更小的方式鏈接它,然後下面的代碼?在這個例子中,如果價格相同,我想先按價格比較價格,然後按名稱比較價格。如何鏈接groovy的飛船操作員進行多級排序?
class Item implements Comparable {
int price
String name
int compareTo(Item other) {
int result = price <=> other.price
if (result == 0) {
result = name <=> other.name
}
return result
}
}