斯威夫特:有人能解释一下这个语法 `numbers.sort { $0 >$1 }` 对我来说? [英] Swift: Can someone explain this syntax `numbers.sort { $0 > $1 }` for me?

查看:26
本文介绍了斯威夫特:有人能解释一下这个语法 `numbers.sort { $0 >$1 }` 对我来说?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这个问题不是关于$0 是什么意思".我在 swift 文档中了解到 $0 就像索引.

First of all, this question is not about "what does $0 mean". I learnt in swift document that $0 is like index.

我的问题是如何使用 numbers.sort { $0 > $1 } 来实现排序功能".我搜索了这种语法 numbers.sort { $0 >$1 } 在其他一些网站中,例如 这个.这显然不是当前版本.所以我仍然无法理解它的含义.

My question is "How numbers.sort { $0 > $1 } can be used to implement a sort function". I searched for this syntax numbers.sort { $0 > $1 } in some other websites, for example this one. It is apparently not the current version. So I still can't understand what the meaning of it.

print(numbers) //[20, 19, 1, 12]
let sortedNumbers = numbers.sort { $0 > $1 }
print(sortedNumbers) //[20, 19, 12, 1]

有人可以为我解释一下上面这段简单的代码吗?就像这个简单的代码 $0 >$1 实现排序功能,将数字从大到小排序.

Can someone explain this simple piece of code above for me? Like how this simple code $0 > $1 implement the sort function, sorting the numbers from big to small.

我对索引有一些了解,这个 $0 看起来像索引,但它只有 $0$1 两个索引.那么怎么才能用成4个数字呢?根据我之前对C++的了解,我无法理解其中的原理.

I know some about index, and this $0 looks like index, but it only has $0 and $1 two indices. So how can it be used into 4 numbers? According to my knowledge in C++ before, I can't understand the principle in this.

请尽可能使您的回答具体.谢谢!

Please make your answer as specific as possible. Thank you!

----------------- 下面是编辑的额外部分-------------------

----------------- Below is edited extra part -------------------

我不知道 stackoverflow 是否允许我像这样编辑我的问题,但是这个额外的部分太长了,所以我不能在评论中添加它.@pbodsk @Paul Richter

I don't know whether stackoverflow would allow me to edit my question like this, but this extra part is too long, so I can't add it in the comment. @pbodsk @Paul Richter

那么 swift 中的 sort() 语法是使用快速排序来处理排序函数的?

So the sort() syntax in swift uses quick sort to deal with sort function?

实际上我的问题更多是关于sort{$0 > $1}操作原理是什么".我知道你上面的意思,我认为这与 swift 2.1 文档所说的相似,但你的答案并不是我真正想知道的.对不起,我的英文表达不是很好.让我尝试另一种方式.

Actually my question is more about "what is the operating principle of sort{$0 > $1}". I know what you mean above, and I think it's similar with what swift 2.1 document says, but your answer is not what I really want to know. Sorry, my English expression is not very good. Let me try another way.

我之前学C++的时候,总会有一些文档解释一个函数的工作原理是什么,或者这个函数(比如这里的sort())是如何在后台运行的.Sort() 这里需要比较第一次和第二次交换.在 C++ 中,就像

When I learnt C++ before, there are always some documents to explain what a function's operating principle is or how this function (like sort() here) operate in background. Sort() here needs to compare first and second interchange. In C++, it's like

if numbers[1] < numbers[2]{ //just consider this pseudocode
    int k;
    k = numbers[1];
    numbers[1] = numbers[2];
    numbers[2] = k;
}

我们可以看到这个过程是显而易见的.很快,就像

We can see this process is obvious. In swift, it's like

numbers.sort({(val1: Int, val2: Int) -> Bool in
   return val1 > val2
})

在哪里比较?它是如何互换的? 是否返回 val1 >val2 自动比较和交换这两个值并返回它们?仅此一种语法就实现了所有 3 个进程?如何?这是我真正想知道的.再次为我糟糕的英语表达感到抱歉.

Where is it compared? And how is it interchanged? Does return val1 > val2 automatically compare and interchange these two values and return them? Just this one syntax implement these all 3 processes? How? This is what I really want to know. Sorry again for my poor English expression.

推荐答案

@the_UB 和 @moonvader 都是对的,但我只是想我会稍微扩展一下@moonvader 的例子,只是为了向你展示我们是如何结束的$0 >$1

@the_UB and @moonvader are both right, but I just thought that I would extend the example from @moonvader a bit, just to show you how we end up with $0 > $1

如果您查看The Swift Programming Language"中关于 闭包表达式 你可以看到,为了对数组进行排序,你调用了 sort 方法,然后该方法可以将一个函数作为参数.

If you look at the example in "The Swift Programming Language" about Closure Expressions you can see that to sort an array you call the sort method which can then take a function as a parameter.

这个函数必须接受两个参数并比较它们,然后返回一个布尔值.

This function must take two parameters and compare them, and then return a boolean.

所以如果我们有这个数组:

So if we have this array:

让数字 = [4, 6, 8, 1, 3]

还有这个方法

func sortBackwards(val1: Int, val2: Int) -> Bool {
   print("val1: \(val1) - val2: \(val2)" )
   return val1 > val2
}

我们可以像这样对元素进行排序:

We can sort the elements like so:

numbers.sort(sortBackwards)//给我们 [8, 6, 4, 3, 1]

sort 方法将对数组中的每个元素使用我们的 sortBackwards 方法并比较它们.

The sort method will use our sortBackwards method on each of the elements in the array and compare them.

这是print

val1: 6 - val2: 4
val1: 8 - val2: 4
val1: 8 - val2: 6
val1: 1 - val2: 4
val1: 3 - val2: 1
val1: 3 - val2: 4

好的,让我们减少它.

我们可以将其作为参数直接添加到 sort 方法中,而不是定义一个函数,如下所示:

Instead of defining a function, we can add that directly as a parameter to the sort method like so:

numbers.sort({(val1: Int, val2: Int) -> Bool in
   return val1 > val2
})

我们还是得到了 [8, 6, 4, 3, 1](太幸运了!)

And we still end up with [8, 6, 4, 3, 1] (how fortunate!)

好的,接下来我们可以做的是The Swift Programming Language"(上面的链接)中称为从上下文推断类型"的内容.当我们在 Int 数组上调用这个方法时,Swift 可以确定我们的 val1val2 参数必须是 Ints 也是,我们不需要告诉它.所以,让我们删除类型.这给我们留下了:

OK, the next thing we can do is what in "The Swift Programming Language" (the link above) is called "Infering Type From Context". As we call this method on an array of Ints, Swift can figure out that our val1 and val2 parameters must be Ints too, there's no need for us to tell it. So, lets remove the types. That leaves us with:

numbers.sort({val1, val2 in
   return val1 > val2
})

结果还是一样.

好的,到了.接下来我们可以做的是书中所说的单表达式闭包的隐式返回"

OK, getting there. The next thing we can do is what in the book is called "Implicit Returns from Single-Expression Closures"

因为我们的比较可以在一行中完成,所以我们不需要使用 return.所以:

As our comparison can be done in one line there's no need for us to use return. So:

numbers.sort({val1, val2 in val1 > val2})

仍然给我们 [8, 6, 4, 3, 1]

Still gives us [8, 6, 4, 3, 1]

最后我们得到了@moonvader 用更少的词来解释的内容:-) 即简写参数名称"

Finally we're getting to what @moonvader used much much less words to explain :-) namely "Shorthand Argument Names"

正如书中所说:

Swift 自动为内联闭包提供速记参数名称,可用于通过名称 $0、$1、$2 等引用闭包参数的值.

Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0, $1, $2, and so on.

因此,在我们的示例中,val1 可以替换为 $0,而 val2 可以替换为 $1

So, in our example, val1 can be replaced by $0 and val2 can be replaced by $1

这给了我们:

numbers.sort({$0 > $1})

我们仍然得到 [8, 6, 4, 3, 1]

And still we get [8, 6, 4, 3, 1]

然后我们可以继续使用Trailing Closure",这意味着如果函数的最后一个参数是一个闭包,我们可以在函数的外部"添加该参数.

We can then continue to use a "Trailing Closure", which means that if the last parameter of a function is a closure, we can add that parameter "outside" the function.

所以我们最终得到:

numbers.sort{$0 > $1}

结果还是 [8, 6, 4, 3, 1]

And the outcome is still [8, 6, 4, 3, 1]

希望有助于澄清事情.

这篇关于斯威夫特:有人能解释一下这个语法 `numbers.sort { $0 &gt;$1 }` 对我来说?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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