我有一个包含数据的数组,因为在通过Firebase获取数据时,有时数据以正确的顺序出现,而有时是随机的。所以,需要使用排序方法对我的本地数组进行排序
Repsonse from snapshot.value :
Optional({
Images = {
"-KmRCabAyzoVNMD06Hpr" = {
UploadTime = 1497269431993;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962226641.jpg?alt=media&token=645878ac-1124-44b0-8203-75e94813ca76";
};
"-KmRCnYSsjJaZ3RINRDG" = {
UploadTime = 1497269484338;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962275721.jpg?alt=media&token=d0113b12-531c-48a5-838b-02f96396a6df";
};
"-KmRCweslwMPBt0aCBIc" = {
UploadTime = 1497269521676;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962317708.jpg?alt=media&token=e931d9b3-5635-4e2f-a597-2c60aa25b38d";
};
"-KmRD13t3pI21ci45LEv" = {
UploadTime = 1497269544342;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962340349.jpg?alt=media&token=a738df76-e00b-48c2-9983-1e902e285a5f";
};
"-KmRD6WfaNTgKaVhy_Iq" = {
UploadTime = 1497269566141;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962359997.jpg?alt=media&token=5828ee44-1721-4267-99d7-91f10381aa13";
};
"-KmREsVwMb4sY6yN4pNK" = {
UploadTime = 1497270028928;
imageUrl = "https://firebasestorage.googleapis.com/v0/b/mychat-1147d.appspot.com/o/Images%2FwRHmnmfu3CeqXa77YgHEWVXI1AI3%2F518962824146.jpg?alt=media&token=aa0383c7-7d26-4b7a-b9bb-dbb1da3cdfff";
};
};
UserInfo = {
UserID = wRHmnmfu3CeqXa77YgHEWVXI1AI3;
userName = "Aric D'sooza";
};
})
我需要根据上传时间对这个数组进行排序。
我试过这个:
let sortedArray = (myArray ).sorted(by: { (dictOne, dictTwo) -> Bool in
let d1 = dictOne["UploadTime"]! as! Double
let d2 = dictTwo["UploadTime"]! as! Double
return d1 < d2
})
print("Sorted Array - %@",sortedArray)
}
并且还尝试过:
self.sortedArray = (myArray as NSArray).sortedArray(using: [NSSortDescriptor(key: "UploadTime", ascending: true)]) as! [[String:AnyObject]]
试试这段代码,它对我有用。
func sortDictonary(aDataDict:[String:AnyObject]) ->[[String:Any]] {
let allKeys = Array(aDataDict.keys)
var arrImages = [[String:Any]] ()
for key in allKeys{
var aDict = aDataDict[key] as! [String:AnyObject]
let aTempData = ["uploadTime" :aDict["UploadTime"] as! Double,
"imageUrl":aDict["imageUrl"] as! String,
"AutoId": key] as [String : AnyObject]
// Here AutoID is unique key from your Response e.g. -KmREsVwMb4sY6yN4pNK
arrImages.append(aTempData)
}
return arrImages
}
调用此函数并将Images Dictionary
传递为 ,
var aDataDict = yourJSONResponse as! Dictionary<String,AnyObject>
var aDictUserInfo = aDataDict["UserInfo"] as!Dictionary<String,AnyObject>
let aDictImages = aDataDict["Images"] as! Dictionary<String,AnyObject>
let arrTempImages :[[String:Any]] = self.sortDictonary(aDataDict: aDictImages)
你会得到排序的数组,作为,
arrImagesData = (arrImagesData as NSArray).sortedArray(using: [NSSortDescriptor(key: "uploadTime", ascending: true)]) as! [[String:AnyObject]]
尝试以下代码:
var localArray : [[String : AnyObject]] = [[:]]
for dic in myArray{
localArray.append(dic)
}
let sortedArray = (localArray ).sorted(by: { (dictOne, dictTwo) -> Bool in
let d1 = dictOne["UploadTime"]! as! Double
let d2 = dictTwo["UploadTime"]! as! Double
return d1 < d2
})
print("Sorted Array - %@",sortedArray)
}
Images
键的关联值是字典而不是数组,字典本身无法排序。您有一些选择,包括:
- 如果键(
"-KmRCabAyzoVNMD06Hpr"
)对您不重要,则将字典转换为数组,然后对该数组进行排序。您在问题中的比较函数可用于此目的。 - 创建键的集合,并根据它们引用的字典中的
UploadTime
键值对它们进行排序。
(2)听起来可能很复杂,但您在问题和后续评论中都有所需的部分:
但是我们可以像这样对字典进行排序:let sortedKeys = Array(dictionary.keys).sorted(<)
这不是对字典进行排序,而是对字典的键进行排序。但是你不希望按键的值对键进行排序,而是根据它们所指的内容 - 元素字典 - 并且你已经按照这些行编写了一个函数,你只需要将两者结合起来:
let orderedKeys = Images.keys.sorted(by: { (keyOne, keyTwo) -> Bool in
let d1 = Images[keyOne]!["UploadTime"] as! Int
let d2 = Images[keyTwo]!["UploadTime"] as! Int
return d1 < d2
})
这获取键(keyOne
),获取它们引用的字典(Images[keyOne]!
),以及您希望排序的字典中的值(["UploadTime"] as! Int
[我的测试数据包含整数]),并进行比较。
请注意,原始字典Images
根本没有排序;相反,您有一个集合orderedKeys
,它按您希望处理字典中值的顺序包含键。要获取排序中的第一个元素,您需要获取orderedKeys
中的第一个键并使用它来索引Images
。
您可以使用orderedKeys
创建一个数组,或者只是打印出值,以便您知道它的工作原理:
for key in orderedKeys
{
print(Images[key] ?? "should never occur")
}
呵呵