在swift中按字母顺序加载联系人列表

时间:2016-03-30 07:49:38

标签: ios swift contacts

我在视图控制器的负载电话联系人中遇到小问题,我在void find_unused_cellports(Module *module) { SigMap sigmap(module); pool<SigBit> used_sigbit; for (auto wire : module->wires()) { if (wire->port_output) for (auto bit : sigmap(wire)) used_sigbit.insert(bit); } for (auto cell : module->cells()) for (auto conn : cell->connections()) if (cell->input(conn.first)) for (auto bit : sigmap(conn.second)) used_sigbit.insert(bit); for (auto cell : module->cells()) for (auto conn : cell->connections()) if (cell->output(conn.first)) for (int i = 0; i < GetSize(conn.second); i++) if (used_sigbit.count(sigmap(conn.second[i])) == 0) log("Unused cell port bit: %s.%s.%s[%d]\n", log_id(module), log_id(cell), log_id(conn.first), i); } 中成功加载了我的电话联系人的姓名和号码,但是联系人加载名称是随机的,我希望联系人姓名必须是字母顺序等于数字

以下是我的示例代码:

Tableview

3 个答案:

答案 0 :(得分:3)

您可以使用:

var descriptor: NSSortDescriptor = NSSortDescriptor(key: "name", ascending: true)
var sortedResults: NSArray = contactArray.sortedArrayUsingDescriptors([descriptor])

答案 1 :(得分:3)

正如我从您的代码中假设您的contactArray设置了这样的值:

let contactArray = [["name": "tim", "phone": 123], ["name": "adi", "phone": 1234], ["name": "cora", "phone": 456], ["name": "berta", "phone": 678]]

要按名称对其进行排序,您可以使用sort method

let sortedContactArray = contactArray.sort{ ($0["name"] as! String) < ($1["name"] as! String) }

// result: [["phone": 1234, "name": adi], ["phone": 678, "name": berta], ["phone": 456, "name": cora], ["phone": 123, "name": tim]]

答案 2 :(得分:2)

你也可以用简洁的Swifty方式进行分类(当然如果你使用的是Swift数组而不是NSArray)

contactArray.sortInPlace { $0["name"] < $1["name"] }
相关问题