简体   繁体   English

如何对包含Modal类型对象的NSMutableArray进行排序?

[英]How to sort NSMutableArray that contains object of type Modal?

I've an NSMutableArray (say accountListArray ). 我有一个NSMutableArray(比如accountListArray )。 The content of array is Model (say BankAccountModel with property defaultAcc - Y/N ). 数组的内容是Model(例如具有属性defaultAcc - Y/N BankAccountModel )。

Now I want to sort the content of accountListArray based on default property ie all account that is defaultAcc enabled (Y) should be on top. 现在,我想基于默认属性对accountListArray的内容进行排序,即, all account that is defaultAcc enabled (Y) should be on top.

class BankAccountModel: NSObject {

    static let sharedInstance: BankAccountModel = BankAccountModel()

    var defaultAcc = "" // Y or N
}

How to do it? 怎么做?

Try this : 尝试这个 :

Create Model property as below 如下创建模型属性

class BankAccountModel: NSObject {

    static let sharedInstance: BankAccountModel = BankAccountModel()
    var isDefault: Bool = false

  // assign value
   if let objDefault = dictionary["default"] {
            isDefault = (objDefault as! String) ==  "Y" ? true : false
        }

   // var default = "" // Y or N
}

Sort as below 排序如下

self.arrOfModels.sort {
     return $0.isDefault && !$1.isDefault // isSelected is your flag . you need to change with your flag 
}

OR 要么

Another Solution with string 字符串的另一种解决方案

self.arrModels.sort {
                        return ($0. defaultAcc == "Y") && ($1. defaultAcc == "N")
                    }

Here is my example. 这是我的例子。 It will sort array and after sort person with name 2 and person with name 4 will be in top of the array 它将对数组进行排序,然后对名称为2的人和名称为4的人进行排序

class People{
    var name = ""
    var isMale:Bool = false

    init(name:String,isMale:Bool){
        self.name = name
        self.isMale = isMale
    }
}

var person1 = People(name: "1",isMale: false)
var person2 = People(name: "2",isMale: true)
var person3 = People(name: "3",isMale: false)
var person4 = People(name: "4",isMale: true)

var array = [person1,person2,person3,person4]

array.sort() { Int(NSNumber(value:($0.isMale))) > Int(NSNumber(value:($1.isMale))) }

Additional to the answers above. 除上述答案外。 You can define your model as Comparable, then implement your own >, <, == protocol: 您可以将模型定义为Comparable,然后实现自己的>,<,==协议:

For example: 例如:

class BankAccountModel: Comparable { //or struct
    var accountHolder: String = ""
    var balance: Decimal?
    var enabled: Bool = false

    init(holder: String, balance: Decimal, enabled: Bool) {
        self.accountHolder = holder
        self.balance = balance
        self.enabled = enabled
    }

    static func == (lhs: BankAccountModel, rhs: BankAccountModel) -> Bool {
        return lhs.enabled == rhs.enabled
    }

    static func > (lhs: BankAccountModel, rhs: BankAccountModel) -> Bool {
        return lhs.enabled || (!lhs.enabled && !rhs.enabled)
    }

    static func < (lhs: BankAccountModel, rhs: BankAccountModel) -> Bool {
        return rhs.enabled || (!rhs.enabled && !lhs.enabled)
    }
}

let account1 = BankAccountModel(holder: "Holder 1", balance: Decimal(10.0), enabled: true)
let account2 = BankAccountModel(holder: "Holder 2", balance: Decimal(20.3), enabled: false)
let account3 = BankAccountModel(holder: "Holder 3", balance: Decimal(-1.0), enabled: false)
let account4 = BankAccountModel(holder: "Holder 4", balance: Decimal(0.0), enabled: true)

var allAccounts = [account1, account2, account3, account4]

allAccounts.sort(by: {return $0 > $1})

Swift 3.0 斯威夫特3.0

Hope will work for you. 希望能为您服务。

let sortedArr = accountListArray.sorted(by: {
    let item1 = $0 as! BankAccountModel
    let item2 = $1 as! BankAccountModel

    if item1.defaultAcc > item2.defaultAcc {
        return true
    }
    return false
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM