sorting - Sort NSArray with sortedArrayUsingComparator -


in objective-c can sort nsarray using statement:

nsarray *sortedarray = [persons sortedarrayusingcomparator:^nscomparisonresult(person *p1, person *p2) {     return [p1.name compare:p2.name]; }]; 

i'm unable reproduce same statement swift. found using array.

you can either use swift's built in sort functions or, since swift array bridged nsarray can call sortedarrayusingcomparator swift directly.

using swift's sorted function:

var sortedarray = sorted(persons) {     (obj1, obj2) in      // downcast person needed if persons nsarray or swift array of anyobjects     let p1 = obj1 person     let p2 = obj2 person     return p1.name < p2.name } 

or, using nsarray's sortedarrayusingcomparator:

var sortedarray = persons.sortedarrayusingcomparator {     (obj1, obj2) -> nscomparisonresult in      let p1 = obj1 person     let p2 = obj2 person     let result = p1.name.compare(p2.name)     return result } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -