本文介绍了在Swift 2中排序一系列的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过很多关于SO的文章,我仍然很沮丧!我有一个字典数组,见下文:

I've read many articles on SO and I'm still stumped! I have an array of dictionaries, see below:

var myArray = [[String:AnyObject]]()

myArray.append([
    "caseNumber" : "12349",
    "formType" : "Advanced",
    "caseStatus" : "Approved",
    "caseDetails" : "blah blah",
    "caseLUD" : NSDate(),
    "friendlyName" : "name1"
    ])

myArray.append([
    "caseNumber" : "12345",
    "formType" : "Standard",
    "caseStatus" : "On-Hold",
    "caseDetails" : "blah blah",
    "caseLUD" : NSDate(),
    "friendlyName" : "name2"
    ])

myArray.append([
    "caseNumber" : "12342",
    "formType" : "Normal",
    "caseStatus" : "Rejected",
    "caseDetails" : "blah blah",
    "caseLUD" : NSDate(),
    "friendlyName" : "name3"
    ])

该数据将进入tableView,我希望用户能够通过 caseNumber 或我的 caseLUD (日期)。但是我不知道如何按值重新排列数组。有什么想法吗?所有的帮助赞赏。

This data will go into a tableView and I want the user to be able to decide the sort order, either by caseNumber or my caseLUD (date). But I can't figure out how to re-order the array by values. Any ideas, please? All help appreciated.

推荐答案

caseNumber排序

myArray.sortInPlace { ($0["caseNumber"] as! String) < ($1["caseNumber"] as! String) }

注意,这使用 caseNumber 作为 String 可能会导致令人惊讶的结果(例如12345<2 code>)。您可能要将 caseNumber 存储为 Int ,或将其转换为 Int 在排序时:

Note, this uses caseNumber as a String which could lead to surprising results (for example "12345" < "2"). You might either want to store caseNumber as an Int, or convert it to an Int when sorting:

myArray.sortInPlace { Int($0["caseNumber"] as! String) < Int($1["caseNumber"] as! String) }

caseLUD

myArray.sortInPlace { ($0["caseLUD"] as! NSDate).compare($1["caseLUD"] as! NSDate) == .OrderedAscending}

。预付款将首先给您最早的日期,而 .OrderedDescending 将首先给您最新的日期。

.OrderedAscending will give you earliest date first, and .OrderedDescending will give you latest date first.

如果您将数据存储在 Struct 而不是 Dictionary ,你可以避免混乱的类型转换。

If you stored your data in a Struct instead of a Dictionary, you could avoid the messy typecasting.

struct CaseRecord {
    var caseNumber: Int
    var formType: String
    var caseStatus: String
    var caseDetails: String
    var caseLUD: NSDate
    var friendlyName: String
}

var myArray = [CaseRecord]()

myArray.append(CaseRecord(
    caseNumber : 12345,
    formType : "Advanced",
    caseStatus : "Approved",
    caseDetails : "blah blah",
    caseLUD : NSDate(),
    friendlyName : "name1"
    )
)

myArray.append(CaseRecord(
    caseNumber : 124,
    formType : "Standard",
    caseStatus : "On-Hold",
    caseDetails : "blah blah",
    caseLUD : NSDate(),
    friendlyName : "name2"
    )
)

myArray.append(CaseRecord(
    caseNumber : 13,
    formType : "Normal",
    caseStatus : "Rejected",
    caseDetails : "blah blah",
    caseLUD : NSDate(),
    friendlyName : "name3"
    )
)

myArray.sortInPlace { $0.caseNumber < $1.caseNumber }

myArray.sortInPlace { $0.caseLUD.compare($1.caseLUD) == .OrderedDescending }

这篇关于在Swift 2中排序一系列的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:49