Sorting Array in Swift3

前端 未结 3 2007
遥遥无期
遥遥无期 2021-02-02 01:39

In my code, I have a struct like the following:

struct Object {
    var name: String
    var count: Int

I am now creating an array of 10 Object

3条回答
  •  天涯浪人
    2021-02-02 02:42

    Narusan, maybe this will help you. Let's say you have an array with your struct objects called objArray, then you can order it by the code bellow:

    var objArray = [Object]()
    objArray.append(Object(name:"Steve", count:0))
    objArray.append(Object(name:"Alex", count:1))
    
    objNameSorted = objArray.sorted (by: {$0.name < $1.name})
    objNCountSorted = objArray.sorted (by: {$0.count < $1.count})
    

提交回复
热议问题