arrays - .sort not working in Swift 2.0 -
i'm trying sort arrays of combinations have. multidimensional array, need sort out array that's inside now.
for combination in mycombinations { combination.sort({$0 < $1}) print("\(combination)") }
here's code sort out array , here's result.
["lys", "dyt", "lrt"] ["lys", "dyt", "gbc"] ["lys", "dyt", "lbc"]
i got warning says "result of call 'sort' unused" me out this?
thanks.
in swift 2, sort
sortinplace
(and sorted
sort
), , both methods called on array (they global functions).
when call combination.sort({$0 < $1})
return sorted array, you're not sorting source array in place.
and in example result of combination.sort({$0 < $1})
not assigned variable, that's compiler telling error message.
assign result of sort
:
let sortedarray = combination.sort({$0 < $1}) print(sortedarray)
if want array of sorted arrays, can use map
instead of loop:
let mycombinations = [["lys", "dyt", "lrt"], ["lys", "dyt", "gbc"], ["lys", "dyt", "lbc"]] let sortedcombinations = mycombinations.map { $0.sort(<) } print(sortedcombinations) // [["dyt", "lrt", "lys"], ["dyt", "gbc", "lys"], ["dyt", "lbc", "lys"]]
Comments
Post a Comment