本文介绍了Swift 2中sort和sortInPlace之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图快速使用sortInPlace函数,但是它不起作用.当我使用sort函数而不是sortinplace时.

I have been trying to use the sortInPlace function in swift but it is not working. When I use the sort function instead of sortinplace it works.

请说明这两个功能之间的区别.如果您可以提供小代码示例来演示这两个功能的使用,那将非常有帮助.

Please explain the difference between these 2 function. It would be very helpful if you can provide small code sample demonstrating the use of both functions.

推荐答案

var mutableArray = [19, 7, 8, 45, 34]

// function sort sorts the array but does not change it. Also it has return

mutableArray.sort()

mutableArray
// prints 19, 7, 8, 45, 34

// function sortInPlace will mutate the array. Do not have return

mutableArray.sortInPlace()

mutableArray
// prints 7, 8, 19, 34, 45

这篇关于Swift 2中sort和sortInPlace之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 20:08