我正在尝试按字母顺序和组名的顺序对数组进行排序
我的名单是:
Name: A
Group: cats
Name: B
Group: dogs
Name: C
Group: cats
我已经按照姓名的字母顺序进行了操作,并向我显示了以下顺序:
A
cats
B
dogs
C
cats
但是我偏头痛试图按组对它们进行排序,例如:
A
cats
C
cats
B
dogs
到目前为止,我尝试按字母顺序和组顺序进行操作:
case alpha:
results.sorted { $0.first_name < $1.first_name }
case grouped:
results.sorted { $0.group_name == $1.group_name }
答案 0 :(得分:6)
尝试一下:
results.sorted {($0.group_name, $0.first_name) < ($1.group_name, $1.first_name)}
示例:
struct Item {
var first_name: String
var group_name: String
}
let results = [
Item(first_name: "A", group_name: "cats"),
Item(first_name: "B", group_name: "dogs"),
Item(first_name: "C", group_name: "cats")
]
let sortedResult = results.sorted {($0.group_name, $0.first_name) < ($1.group_name, $1.first_name)}
print(sortedResult)
//->[SortGroups.Item(first_name: "A", group_name: "cats"), SortGroups.Item(first_name: "C", group_name: "cats"), SortGroups.Item(first_name: "B", group_name: "dogs")]
Carpsen90当前代码失败的示例。
struct Animal {
let Name: String
let Group: String
}
let animals = [
Animal(Name: "B", Group: "rats"),
Animal(Name: "C", Group: "dogs"),
Animal(Name: "E", Group: "cats"),
Animal(Name: "B", Group: "dogs"),
Animal(Name: "A", Group: "cats"),
Animal(Name: "C", Group: "dogs"),
Animal(Name: "E", Group: "cats"),
Animal(Name: "A", Group: "dogs"),
Animal(Name: "B", Group: "dogs"),
Animal(Name: "B", Group: "dogs"),
Animal(Name: "D", Group: "cats"),
Animal(Name: "A", Group: "cats"),
Animal(Name: "B", Group: "cats"),
Animal(Name: "B", Group: "dogs"),
Animal(Name: "D", Group: "rats"),
Animal(Name: "A", Group: "rats"),
Animal(Name: "B", Group: "dogs"),
Animal(Name: "D", Group: "dogs"),
Animal(Name: "A", Group: "dogs"),
Animal(Name: "A", Group: "rats")
]
let result = animals.sorted(by: {$0.Group < $1.Group}).sorted(by: {$0.Group == $1.Group && $0.Name < $1.Name })
print("[\n "+result.map {String(describing: $0)}.joined(separator: ",\n ")+"\n]")
输出:
[ Animal(Name: "E", Group: "cats"), Animal(Name: "A", Group: "dogs"), Animal(Name: "A", Group: "dogs"), Animal(Name: "B", Group: "dogs"), Animal(Name: "B", Group: "dogs"), Animal(Name: "B", Group: "dogs"), Animal(Name: "B", Group: "dogs"), Animal(Name: "B", Group: "dogs"), Animal(Name: "C", Group: "dogs"), Animal(Name: "C", Group: "dogs"), Animal(Name: "A", Group: "cats"), Animal(Name: "B", Group: "cats"), Animal(Name: "D", Group: "cats"), Animal(Name: "E", Group: "cats"), Animal(Name: "D", Group: "dogs"), Animal(Name: "A", Group: "cats"), Animal(Name: "A", Group: "rats"), Animal(Name: "A", Group: "rats"), Animal(Name: "B", Group: "rats"), Animal(Name: "D", Group: "rats") ]
对相同的输入使用元组比较:
let result2 = animals.sorted {($0.Group, $0.Name) < ($1.Group, $1.Name)}
print("[\n "+result2.map {String(describing: $0)}.joined(separator: ",\n ")+"\n]")
输出:
[ Animal(Name: "A", Group: "cats"), Animal(Name: "A", Group: "cats"), Animal(Name: "B", Group: "cats"), Animal(Name: "D", Group: "cats"), Animal(Name: "E", Group: "cats"), Animal(Name: "E", Group: "cats"), Animal(Name: "A", Group: "dogs"), Animal(Name: "A", Group: "dogs"), Animal(Name: "B", Group: "dogs"), Animal(Name: "B", Group: "dogs"), Animal(Name: "B", Group: "dogs"), Animal(Name: "B", Group: "dogs"), Animal(Name: "B", Group: "dogs"), Animal(Name: "C", Group: "dogs"), Animal(Name: "C", Group: "dogs"), Animal(Name: "D", Group: "dogs"), Animal(Name: "A", Group: "rats"), Animal(Name: "A", Group: "rats"), Animal(Name: "B", Group: "rats"), Animal(Name: "D", Group: "rats") ]