简体   繁体   中英

How to sort NSMutableArray that contains object of type Modal?

I've an NSMutableArray (say accountListArray ). The content of array is Model (say BankAccountModel with property defaultAcc - Y/N ).

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.

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

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:

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

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
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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