objective c - Can anyone provide me an example of how to sort an array by date? -


I have an array that stores events received through an object; I want to sort them according to date I have read that "sortedArrayUsingSelector: on the array, to provide the selector for comparison: the method ', but I do not know how to use it at all. Does anyone provide me an example ?

< / Div> P> Once you take your array, consider:

  NSArray * sortedArray = [Event sortedUruseingcomputer: ^ NSCOPER ARIAN RESULT (EKEVENT * EVENT 1, EKEVENT * EVENT 2) {Return of Event 1. Start Date: Event 2. StartDate  

- sortedArrayUsingComparator NSComparator block, which is defined as the following:

  Type-cap NSC transit Result (^ nscampiator) (id ogje1, id ogje2);  

You can basically think about it as a block (a lambda-function) There are two logic arguments, and you tell a NSComparisonResult member that the order of two objects given is dependent on the type of object that you have in your array. Inclined, in this case, EKEvent s depends on you how you sort them; For more information and rules, read on NSArray will call your block several times, every time two items will be presented, unless the ARA is classified.

Now, lucky you, EKEvent , exposes a selector who knows how to compare to EKEvents NSArray There is another way you can use, and you can tell it only to use the select selector which reveals EKEvent:

  NSArray * sortedArray = [events sortedArrayUsingSelector : @selector (compareStartDateWithEvent :)];  

will now call NSArray - compareStartDateWithEvent: Each time it wants to compare two objects if you want it, you can You can use it. Otherwise, use the comparator method above.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -