按键值对字典的Swift数组进行排序 [英] Sort Swift Array of Dictionaries by Value of a Key

查看:131
本文介绍了按键值对字典的Swift数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对由字典组成的Swift数组进行排序.我在下面准备了一个工作示例.目的是通过字典中的"d"元素对整个数组进行排序.我已经准备了可以在Swift项目中放置的工作示例:

I am attempting to sort a Swift array which is composed of dictionaries. I have prepared a working example below. The goal is to sort the entire array by the "d" element in the dictionaries. I have prepared this working example which can be placed into a Swift project:

var myArray = Array<AnyObject>()
var dict = Dictionary<String, AnyObject>()

dict["a"] = "hickory"
dict["b"] = "dickory"
dict["c"] = "dock"
dict["d"] = 5

myArray.append(dict)

dict["a"] = "three"
dict["b"] = "blind"
dict["c"] = "mice"
dict["d"] = 6

myArray.append(dict)

dict["a"] = "larry"
dict["b"] = "moe"
dict["c"] = "curly"
dict["d"] = 2

myArray.append(dict)

println(myArray[0])
println(myArray[1])
println(myArray[2])
}

这将导致以下输出到日志:

This results in the following output to the log:

{
    a = hickory;
    b = dickory;
    c = dock;
    d = 5;

}
{
    a = three;
    b = blind;
    c = mice;
    d = 6;
}
{
    a = larry;
    b = moe;
    c = curly;
    d = 2;
}

目标是按"d"元素对数组进行排序,以便将上面的输出更改为以下内容(基于"d"的数字顺序:"2、5、6"):

The goal is to sort the array by the "d" element, so that the above output would be changed to the following (which is based on numerical order of "d": '2, 5, 6'):

{
    a = larry;
    b = moe;
    c = curly;
    d = 2;
}
{
    a = hickory;
    b = dickory;
    c = dock;
    d = 5;
}
{
    a = three;
    b = blind;
    c = mice;
    d = 6;
}

还有其他一些看起来很相似的问题,但是当您查看它们时,很明显他们没有解决这个问题.谢谢您的协助.

There are some other questions that seem similar, but when you look at them, it becomes clear they are not addressing this. Thank you for your assistance.

推荐答案

要声明,如果需要将其保留为AnyObject,则必须显式强制转换:

To declare, if you need to keep it as AnyObject, you have to explicitly cast:

var myArray = Array<AnyObject>()
var dict = Dictionary<String, AnyObject>()

dict["a"] = ("hickory" as! AnyObject)
dict["b"] = ("dickory" as! AnyObject)
dict["c"] = ("dock" as! AnyObject)
dict["d"] = (6 as! AnyObject)

myArray.append(dict as! AnyObject)

dict["a"] = ("three" as! AnyObject)
dict["b"] = ("blind" as! AnyObject)
dict["c"] = ("mice" as! AnyObject)
dict["d"] = (5 as! AnyObject)


myArray.append(dict as! AnyObject)

dict["a"] = ("larry" as! AnyObject)
dict["b"] = ("moe" as! AnyObject)
dict["c"] = ("curly" as! AnyObject)
dict["d"] = (4 as! AnyObject)

myArray.append(dict as! AnyObject)

无需附加,您可以这样做:

Without appending, you can do it like this:

var myArray: [AnyObject] = [ ([
    "a" : ("hickory" as! AnyObject),
    "b" : ("dickory" as! AnyObject),
    "c" : ("dock" as! AnyObject),
    "d" : (6 as! AnyObject)
  ] as! AnyObject), ([
    "a" : ("three" as! AnyObject),
    "b" : ("blind" as! AnyObject),
    "c" : ("mice" as! AnyObject),
    "d" : (5 as! AnyObject)
  ] as! AnyObject), ([
    "a" : ("larry" as! AnyObject),
    "b" : ("moe" as! AnyObject),
    "c" : ("curly" as! AnyObject),
    "d" : (4 as! AnyObject)
  ] as! AnyObject)
]

哪个会给您相同的结果.虽然,如果仅需要更改字典中的value对象,则无需强制转换数组的元素:

Which gives you the same result. Although, if only the value object in the dictionary needs to change, you don't need to cast the elements of the array:

var myArray: [Dictionary<String, AnyObject>] = [[
    "a" : ("hickory" as! AnyObject),
    "b" : ("dickory" as! AnyObject),
    "c" : ("dock" as! AnyObject),
    "d" : (6 as! AnyObject)
  ], [
    "a" : ("three" as! AnyObject),
    "b" : ("blind" as! AnyObject),
    "c" : ("mice" as! AnyObject),
    "d" : (5 as! AnyObject)
  ], [
    "a" : ("larry" as! AnyObject),
    "b" : ("moe" as! AnyObject),
    "c" : ("curly" as! AnyObject),
    "d" : (4 as! AnyObject)
  ]
]

然后,要排序,请使用sort()闭包,该闭包将对Array进行排序.您提供的闭包有两个参数(分别为$ 0和$ 1),并返回一个布尔值.如果在$ 1之前订购了$ 0,则闭包应返回true;如果在$ 1之后订购,则应返回false.为此,您必须进行大量操作:

Then, to sort, you use the sort() closure, which sorts an Array in place. The closure you supply takes two arguments (named $0 and $1), and returns a Bool. The closure should return true if $0 is ordered before $1, or false if it comes after. To do this, you've got to cast an awful lot:

//myArray starts as: [
//  ["d": 6, "b": "dickory", "c": "dock", "a": "hickory"],
//  ["d": 5, "b": "blind", "c": "mice", "a": "three"],
//  ["d": 4, "b": "moe", "c": "curly", "a": "larry"]
//]

myArray.sort{
  (($0 as! Dictionary<String, AnyObject>)["d"] as? Int) < (($1 as! Dictionary<String, AnyObject>)["d"] as? Int)
}

//myArray is now: [
//  ["d": 4, "b": "moe", "c": "curly", "a": "larry"],
//  ["d": 5, "b": "blind", "c": "mice", "a": "three"],
//  ["d": 6, "b": "dickory", "c": "dock", "a": "hickory"]
//]

这篇关于按键值对字典的Swift数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆