美文网首页IOS 学习
swift 集合的排序 与 洗牌

swift 集合的排序 与 洗牌

作者: hiasyou | 来源:发表于2023-01-17 16:24 被阅读0次

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"]

相关文章

网友评论

    本文标题:swift 集合的排序 与 洗牌

    本文链接:https://www.haomeiwen.com/subject/mhzthdtx.html