sorted function in Swift 2

前端 未结 5 648
心在旅途
心在旅途 2020-12-19 14:09

I\'m sorting an Array like this:

var users = [\"John\", \"Matt\", \"Mary\", \"Dani\", \"Steve\"]

func back (s1:String, s2:String) -> Bool
{
    return s1         


        
5条回答
  •  离开以前
    2020-12-19 14:35

    Follow what the error message is telling you, and call sort on the collection:

    users.sort(back)
    

    Note that in Swift 2, sorted is now sort and the old sort is now sortInPlace, and both are to be called on the array itself (they were previously global functions).

    Be careful, this has changed again in Swift 3, where sort is the mutating method, and sorted is the one returning a new array.

提交回复
热议问题