我有一系列元组。我想按元组的第一个元素对列表进行排序。
示例:
var names = [("C","MTS",4),("A","MTS",3),("M","SMTS",1),("N","LEAD",5)];
我想通过元组的第一个元素按以下顺序对它们进行排序
[("A","MTS",3),("C","MTS",4),("M","SMTS",1),("N","LEAD",5)];
请建议。
答案 0 :(得分:1)
试试这个:
var names = [("C","MTS",4),("A","MTS",3),("M","SMTS",1),("N","LEAD",5)]
names.sortInPlace{ $0.0 < $1.0 }
// You can use this if it more clearly:
// names.sortInPlace { (name1: (String, String, Int), name2: (String, String, Int)) -> Bool in
// return name1.0 < name2.0
//}
print(names)
//[("A", "MTS", 3), ("C", "MTS", 4), ("M", "SMTS", 1), ("N", "LEAD", 5)]