Difference between sort and sortInPlace in swift 2?

前端 未结 2 1558
有刺的猬
有刺的猬 2021-01-18 11:48

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 d

2条回答
  •  无人共我
    2021-01-18 12:02

    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
    

提交回复
热议问题