Why is compiler forcing me to use sorted(by:) in S

2019-09-11 15:25发布

I am using Swift 3. I have a dictionary of arrays, and I am trying to sort the arrays in place using a property each object in the array contains.

As far as I understand it as of Swift 3 sorted() does not sort in place, but instead returns a sorted array value. If you want to sort in place you should use sort(). But when I try the compiler keeps saying "sort() has been renamed sorted(by:)

Why won't the compiler let me use sort()

Here is my code:

func sortAllArraysInDict() {

    for arrayOfGrowthPaths in catDict.values {

        arrayOfGrowthPaths.sort({$0.growthPathDisplayOrder < $1.growthPathDisplayOrder})
}

1条回答
成全新的幸福
2楼-- · 2019-09-11 15:28

Make sure you declare your array as variable and just delete the parentheses (trailing closure syntax):

arrayOfGrowthPaths.sort { $0.growthPathDisplayOrder < $1.growthPathDisplayOrder }
查看更多
登录 后发表回答