objective c - Can anyone provide me an example of how to sort an array by date? -
i have array stores events fetched via predicate; want sort them date. read have "call sortedarrayusingselector: on array, providing selector comparestartdatewithevent: method', don't know how utilize exactly. provide me example?
here's array: nsarray *events = [store eventsmatchingpredicate:predicate]
thanks!
once have array, consider:
nsarray *sortedarray = [events sortedarrayusingcomparator:^nscomparisonresult(ekevent *event1, ekevent *event2) { homecoming [event1.startdate compare:event2.startdate]; }];
this sort events start date, using built-in nsdate - compare function.
the - sortedarrayusingcomparator
method takes nscomparator
block, defined as:
typedef nscomparisonresult (^nscomparator)(id obj1, id obj2);
you can think of block (a lambda-function) takes 2 arguments, , homecoming nscomparisonresult
fellow member explains ordering of 2 given objects are. type of object depends on shoved array, in case ekevent
s. how sort them you; read on nscomparator more info , rules. nsarray
phone call block multiple times, presenting 2 items each time, until array sorted.
now, lucky you, ekevent
exposes selector knows how compare ekevents
. nsarray
has method, sortedarrayusingselector: can use, , tell utilize comparing selector ekevent exposes:
nsarray *sortedarray = [events sortedarrayusingselector:@selector(comparestartdatewithevent:)];
now nsarray
phone call - comparestartdatewithevent:
each time wants compare 2 items. if ordering want, can utilize it. otherwise, utilize comparator method above.
objective-c eventkit
No comments:
Post a Comment