Sorting of an array alphabetically in swift

后端 未结 7 1301
感动是毒
感动是毒 2020-12-23 14:03

I am new to swift.I am trying one sample app in which I need to implement the sorting of an array in alphabetical order.I getting the json data and I am adding the titles in

相关标签:
7条回答
  • 2020-12-23 14:50

    First convert NSMutableArray to the Array by using below line of code.

    let swiftArray = mutableArray as AnyObject as! [String]
    

    Use below line of code to sort the Array.

    var sortedArray = names.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
    

    Check below link for sort Closures. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

    Update for Swift 3.0

    var sortedArray = swiftArray.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedAscending }
    
    0 讨论(0)
提交回复
热议问题