Garima Saini Garima Saini -4 years ago 66
iOS Question

Sorting Array of Dictionary getting from snapshot.value using Firebase SDK in swift 3.1

I have an array which contains data as while fetching through firebase sometimes data come in proper sequence while some time randomly. So, needed to sort my local array using sorting method

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";
};
})


I need to sort this array according to UploadTime.

I have tried this:

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)
}


and also tried:

self.sortedArray = (myArray as NSArray).sortedArray(using: [NSSortDescriptor(key: "UploadTime", ascending: true)]) as! [[String:AnyObject]]

Answer Source

Try this code , It worked for me.

 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
    }

Call this function and pass your Images Dictionary as ,

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)

You will get sorted array as,

arrImagesData = (arrImagesData as NSArray).sortedArray(using: [NSSortDescriptor(key: "uploadTime", ascending: true)]) as! [[String:AnyObject]]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download