objective c - sorting NSMutableArray twice -


i have sorted nsmutablearray, has dictionaries many fields . 2 fields interested me , date , count integer number .

i have sorted array date :

 nssortdescriptor *descriptor = [[nssortdescriptor alloc] initwithkey:@"datecreated"  ascending:yes];    sortedwithdates=[sortedwithdates sortedarrayusingdescriptors:[nsarray arraywithobjects:descriptor,nil]]; 

now, want take objects in array -for today, , sort them again according count integer(highest first) .

is there simple way same nssortdescriptor class ?

thank .

is there simple way same nssortdescriptor class ?

yes, there's very simple way. can pass more 1 sort descriptor -sortedarrayusingdescriptors:. items equal according first descriptor sorted according second (or third, etc.).

from docs:

the first descriptor specifies primary key path used in sorting receiving array’s contents. subsequent descriptors used further refine sorting of objects duplicate values.

so code this:

nssortdescriptor *date = [[nssortdescriptor alloc] initwithkey:@"datecreated"  ascending:yes]; nssortdescriptor *count = [[nssortdescriptor alloc] initwithkey:@"count"  ascending:yes]; sortedwithdates=[sortedwithdates sortedarrayusingdescriptors:@[date, count]]; 

note: used newer object literal syntax array of descriptors. that's matter of style -- +arraywithobjects: call fine too.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -