Swift 3对一组元组进行排序(Swift 3 sorting an array of tuples)

编程入门 行业动态 更新时间:2024-10-21 14:28:46
Swift 3对一组元组进行排序(Swift 3 sorting an array of tuples)

我找到了这些答案:

在swift中对元组数组 进行排序 3 如何对 元组数组 进行排序?

但我还有问题。 这是我的代码:

var countsForLetter:[(count:Int, letter:Character)] = [] ... countsForLetter.sorted(by: {$0.count < $1.count})

Swift 3希望我添加by:现在它说,调用sorted:by的结果是未使用的。

我是新来的swift 3.对不起,如果这是一个基本的问题。

I found these answers:

Sort an array of tuples in swift 3 How to sort an Array of Tuples?

But I'm still having issues. Here is my code:

var countsForLetter:[(count:Int, letter:Character)] = [] ... countsForLetter.sorted(by: {$0.count < $1.count})

Swift 3 wanted me to add the by: and now it says that the result of the call to sorted:by is unused.

I'm new to swift 3. Sorry if this is a basic question.

最满意答案

你得到这个警告是因为sorted(by...返回一个新的排序后的数组版本,所以这个警告指出你没有把它分配给一个变量或任何东西,你可以说:

countsForLetter = countsForLetter.sorted(by: {$0.count < $1.count})

我怀疑你试图“排序”,所以在这种情况下,你可以改变sorted(by sort(by它,它只是对countsForLetter排序并将其分配给相同的变量。

You are getting that warning because sorted(by... returns a new, sorted version of the array you call it on. So the warning is pointing out the fact that you're not assigning it to a variable or anything. You could say:

countsForLetter = countsForLetter.sorted(by: {$0.count < $1.count})

I suspect that you're trying to "sort in place", so in that case you could change sorted(by to sort(by and it would just sort countsForLetter and leave it assigned to the same variable.

更多推荐

本文发布于:2023-07-31 00:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340525.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:sorting   Swift   tuples   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!