sort array of anyobject Swift 3

2019-03-03 17:44发布

I am trying to sort an array of anyobject, but not able to do it.I get some data from Parse database in AnyObject format. As per below data, I want to sort this AnyObject array by "NAME". Below is my code -

  let sortedArray = (myArray as! [AnyObject]).sorted(by: { (dictOne, dictTwo) -> Bool in
                                                            let d1 = dictOne["NAME"]! as AnyObject; // this line gives error "Ambiguous use of subscript"
                                                            let d2 = dictTwo["NAME"]! as AnyObject; // this line gives error "Ambiguous use of subscript"

                                                            return d1 < d2
                                                        })

myArray looks like this -

 {
            LINK = "www.xxx.com";
            MENU = Role;
            "MENU_ID" = 1;
            NAME = "A Name";
            SUBMENU = "XXX";
            "Training_ID" = 2;
        },
            {
            LINK = "www.xyz.com";
            MENU = Role;
            "MENU_ID" = 2;
            NAME = "B name";
            SUBMENU = "jhjh";
            "Training_ID" = 6;
        },
            {
            LINK = "www.hhh.com";
            MENU = Role;
            "MENU_ID" = 3;
            NAME = "T name";
            SUBMENU = "kasha";
            "Training_ID" = 7;
        },
            {
            LINK = "www.kadjk.com";
            MENU = Role;
            "MENU_ID" = 5;
            NAME = "V name";
            SUBMENU = "ksdj";
            "Training_ID" = 1;
        },
            {
            LINK = "www.ggg.com";
            MENU = Role;
            "MENU_ID" = 4;
            NAME = "K name";
            SUBMENU = "idiot";
            "Training_ID" = 8;
        },
            {
            LINK = "www.kkk.com";
            MENU = Role;
            "MENU_ID" = 6;
            NAME = "s name";
            SUBMENU = "BOM/ABOM/BSM";
            "Training_ID" = 12;
        }

Any help would be much appreciated. Thanks!

3条回答
在下西门庆
2楼-- · 2019-03-03 18:03

Why are converting array to [AnyObject] instead of that convert the array to the [[String:Any]] means Array of Dictionary and tell the compiler that array contains Dictionary as objects.

if let array = myArray as? [[String:Any]] {
   let sortedArray = array.sorted(by: { $0["NAME"] as! String < $1["NAME"] as! String })
}

Note: As of you are having NAME key with String value in your each dictionaries of array I have force wrapped it with the subscript.

查看更多
放我归山
3楼-- · 2019-03-03 18:11

It's not [AnyObject] (array of I-have-no-idea), it's array of dictionaries [[String:Any]]. Be more specific, this resolves the error.

In Swift 3 the compiler must know the specific type of all subscripted objects.

let sortedArray = (myArray as! [[String:Any]]).sorted(by: { (dictOne, dictTwo) -> Bool in
                                         let d1 = dictOne["NAME"]! as String
                                         let d2 = dictTwo["NAME"]! as String

                                         return d1 < d2
                                     })
查看更多
贼婆χ
4楼-- · 2019-03-03 18:12

You can use below function if you want

//function to sort requests
func sortRequests(dataToSort: [[String:Any]]) -> [[String:Any]] {
    print("i am sorting the requests...")
    var returnData = [[String:Any]]()
    returnData = dataToSort
    returnData.sort{
            let created_date0 = $0["date"] as? Double ?? 0.0
            let created_date1 = $1["date"] as? Double ?? 0.0
            return created_date0 > created_date1
    }
    return returnData
}
查看更多
登录 后发表回答