Swift: Loop through objects and their certain variables -
this question has answer here:
i have class called "man". 1 of variable of man "height" of person.
i have example 10 objects of "man" different values height parameter , want order these objects height. how can achieve this?
var allman:[man] = [man]()     currentman in allman {         //something     } 
let assume allmen array sorted:
var allmen = [man]() then, suppose initialised array 10 values. after can sorted allmen in descending order:
var allsortedmen = allmen.sort { $0.height > $1.height } explanation:
you should pass function/closure of type isorderedbefore: (self.generator.element, self.generator.element) -> bool 
let sortedallmen = allmen.sort { (first: men, second: men -> bool in     return first.height > second.height       // or return first.height < second.height ascending sort order } 
Comments
Post a Comment