这个问题已经有了答案:
Swift how to sort array of custom objects by property value
17个答案
请告诉我如何根据“当前点”值对“bonuscardsarray”中的多个“bonuscard”数组进行排序(降序、升序)。

//Array to sort according to "currentPoints"
var bonusCardsArray = [BonusCard]()

//basis class
class BonusCard {

  var companyName             : String
  var bonusCardDescription    : String
  var currentPoints           : Int
  var bonusCardType           : Int


  init(companyName: String, bonusCardDescription: String,
  currentPoints: Int, bonusCardType: Int) {

      self.companyName = companyName
      self.bonusCardDescription = bonusCardDescription
      self.currentPoints = currentPoints
      self.bonusCardType = bonusCardType
  }
}

最佳答案

要进行适当的排序(按w.r.t.升序到currentPoints属性),请使用sort(by:) method

bonusCardsArray.sort { $0.currentPoints < $1.currentPoints }

或者,创建一个新数组;原始数组的排序版本,使用sorted(by:) method
let sortedfBonusCardsArray = bonusCardsArray
    .sorted { $0.currentPoints < $1.currentPoints }

10-08 04:14