如何在swift

时间:2016-08-10 13:10:27

标签: swift2

我有以下代码按字母顺序按升序排序字典数组。但是我在大写字母后面得到了排序的小写字母键值。恩。如果键的值为" Jast"," rat"," Put" ," put"," Avi"," Que" 。我的输出类似于" Avi",#34; Jast"," Put"," Que"," put" ,"老鼠"哪个没有排序。我使用了以下代码

let sortedArray = ( 
   episode_ParticipantList as NSArray
).sortedArrayUsingDescriptors(
 [NSSortDescriptor(key: "FirstName", ascending: true)])
 as!
 [[String:AnyObject]] 

2 个答案:

答案 0 :(得分:2)

IN SWIFT 4

responseData = responseData.sorted { ($0["firstname"] as! String).localizedCaseInsensitiveCompare($1["firstname"] as! String) == ComparisonResult.orderedAscending }

答案 1 :(得分:1)

怎么样:

let sortedArray = episode_ParticipantList.sort{
    $0["FirstName"]?.lowercaseString < $1["FirstName"]?.lowercaseString
}

这可能会更快,但您需要测试:

import Foundation

let sortedArray = episode_ParticipantList.sort{
    $0["FirstName"]!.caseInsensitiveCompare($1["FirstName"]!) == .OrderedAscending
}