对任何对象 Swift 3 的数组进行排序
sort array of anyobject Swift 3
我正在尝试对 anyobject 的数组进行排序,但无法 it.I 从 Parse 数据库中以 AnyObject 格式获取一些数据。根据以下数据,我想按 "NAME" 对这个 AnyObject 数组进行排序。下面是我的代码 -
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 看起来像这样 -
{
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;
}
如有任何帮助,我们将不胜感激。谢谢!
为什么要将数组转换为 [AnyObject]
而不是将数组转换为 [[String:Any]]
意味着 Array
of Dictionary
并告诉编译器数组包含 Dictionary
作为对象。
if let array = myArray as? [[String:Any]] {
let sortedArray = array.sorted(by: { [=10=]["NAME"] as! String < ["NAME"] as! String })
}
注意: 当你在数组的每个字典中有 NAME
键和 String
值时,我已经用下标强制包装它。
它不是 [AnyObject]
(I-have-no-idea 的数组),它是字典数组 [[String:Any]]
。更具体地说,这解决了错误。
在Swift3中编译器必须知道所有下标对象的具体类型
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
})
如果需要可以使用下面的功能
//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 = [=10=]["date"] as? Double ?? 0.0
let created_date1 = ["date"] as? Double ?? 0.0
return created_date0 > created_date1
}
return returnData
}
我正在尝试对 anyobject 的数组进行排序,但无法 it.I 从 Parse 数据库中以 AnyObject 格式获取一些数据。根据以下数据,我想按 "NAME" 对这个 AnyObject 数组进行排序。下面是我的代码 -
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 看起来像这样 -
{
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;
}
如有任何帮助,我们将不胜感激。谢谢!
为什么要将数组转换为 [AnyObject]
而不是将数组转换为 [[String:Any]]
意味着 Array
of Dictionary
并告诉编译器数组包含 Dictionary
作为对象。
if let array = myArray as? [[String:Any]] {
let sortedArray = array.sorted(by: { [=10=]["NAME"] as! String < ["NAME"] as! String })
}
注意: 当你在数组的每个字典中有 NAME
键和 String
值时,我已经用下标强制包装它。
它不是 [AnyObject]
(I-have-no-idea 的数组),它是字典数组 [[String:Any]]
。更具体地说,这解决了错误。
在Swift3中编译器必须知道所有下标对象的具体类型
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
})
如果需要可以使用下面的功能
//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 = [=10=]["date"] as? Double ?? 0.0
let created_date1 = ["date"] as? Double ?? 0.0
return created_date0 > created_date1
}
return returnData
}