【问题标题】:Swift 3 sort jsonArray [duplicate]Swift 3排序jsonArray [重复]
【发布时间】:2017-06-27 07:45:01
【问题描述】:

请您告诉我如何根据其“currentPoints”值对“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
  }
}

【问题讨论】:

    标签: arrays json swift sorting


    【解决方案1】:

    要就地排序(按currentPoints 属性的升序排列),请使用sort(by:) method

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

    或者,创建一个新数组;原始版本的排序版本,使用sorted(by:) method

    let sortedfBonusCardsArray = bonusCardsArray
        .sorted { $0.currentPoints < $1.currentPoints }
    

    【讨论】:

      【解决方案2】:

      您应该编写以下代码。

      • 降序

        bonusCardsArray.sorted({ $0.currentPoints > $1.currentPoints })

      • 升序

        bonusCardsArray.sorted({ $0.currentPoints

      【讨论】:

      • 非常感谢,伙计们。无法想象单线可以做这么多:-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多