swift 集合的排序 与 洗牌
2023-01-17 本文已影响0人
hiasyou
var numbers = [10, 20, 30, 40, 50]
//无返回值 对数组进行修改
numbers.sort()//排序
numbers.shuffle()//打乱
//返回新的数字不对原数据修改
numbers.sorted()
numbers.shuffled()
排序: 可以用 “>”、"<"
mutating func sort()
var students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
students.sort()
print(students)
// Prints "["Abena", "Akosua", "Kofi", "Kweku", "Peter"]"
students.sort(by: >)
print(students)
// Prints "["Peter", "Kweku", "Kofi", "Akosua", "Abena"]"
打乱:
mutating func shuffle()
var names = ["Alejandro", "Camila", "Diego", "Luciana", "Luis", "Sofía"]
names.shuffle(using: myGenerator)
// names == ["Luis", "Camila", "Luciana", "Sofía", "Alejandro", "Diego"]