Reputation: 18581
I want to sort an array of tuples [(Int,Int)]
based on the .1
element (from largest to smallest), in case of equality compare .0
(from smallest to largest)
I am using this :
myArray.sort{ $0.0 != $1.0 ? $0.1 > $1.1 : $0.0 < $1.0}
but it's not working
Upvotes: 0
Views: 1729
Reputation: 539715
The first term in the conditional expression is wrong. If you want to sort on
the .1-component first (in decreasing order) then you have return
$0.1 > $1.1
if these elements are different, otherwise compare the .0-components:
This should produce the expected result:
myArray.sort{ $0.1 != $1.1 ? $0.1 > $1.1 : $0.0 < $1.0 }
============
difference here
Example:
var myArray = [ (1,1), (1,2), (2,1), (2,2)]
myArray.sort{ $0.1 != $1.1 ? $0.1 > $1.1 : $0.0 < $1.0 }
println(myArray)
// [(1, 2), (2, 2), (1, 1), (2, 1)]
Upvotes: 5