的.sort不雨燕2.0​​工作 [英] .sort not working in Swift 2.0

查看:128
本文介绍了的.sort不雨燕2.0​​工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的组合我现在有数组排序。它是多维数组,但我只需要理清这里面对于现在的数组。

I'm trying to sort my arrays of combinations i currently have. It is multidimensional array, but I only need to sort out the array that's inside for now.

for combination in myCombinations {
        combination.sort({$0 < $1})
        print("\(combination)")
    }

下面是我的code理清阵列,这里是我的结果。

Here's my code to sort out the array and here's my result.

["lys", "dyt", "lrt"]
["lys", "dyt", "gbc"]
["lys", "dyt", "lbc"]

我也有,说一个警告的号召,以排序的结果没有被使用
谁能帮我这个?

I also got a warning that says "Result of call to 'sort' is unused" Could anyone help me out with this?

感谢。

推荐答案

在斯威夫特2,是什么排序现在 sortInPlace (什么是整理现在排序),并且这两种方法都是在阵列本身上被称为(他们是previously全局函数)。

In Swift 2, what was sort is now sortInPlace (and what was sorted is now sort), and both methods are to be called on the array itself (they were previously global functions).

当你调用 combination.sort({$ 0℃; $ 1})你真正的收益的排序后的数组,你不排序源数组中的位置。

When you call combination.sort({$0 < $1}) you actually return a sorted array, you're not sorting the source array in place.

和在你的例子的结果combination.sort({$ 0℃; $ 1})没有分配到任何变量,这就是编译器告诉你这个错误消息。

And in your example the result of combination.sort({$0 < $1}) is not assigned to any variable, that's what the compiler is telling you with this error message.

指定的结果排序

let sortedArray = combination.sort({$0 < $1})
print(sortedArray)


如果你想获得排序数组的数组,你可以使用地图,而不是一个循环:

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"]]

这篇关于的.sort不雨燕2.0​​工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆