objective c - iOS what to use for a sorted list? -


i have been looking @ ios collection classes , trying find sortedlist.

i have custom comparer method compares 2 objects, similar .net sortedlist class.

i not want sort list myself every time need sorted, hoping find class pass compare method 2 , every time add/insert object gets inserted correct location list sorted.

sorry if foolish question, there many different collection classes in ios.

thanks help

foundation has no mutable collection class keeps list sorted.

use nsmutablearray , -insertobject:atindex: add objects. determine correct index using -indexofobject:insortedrange:options:usingcomparator:.

from nsarray reference:

if nsbinarysearchinginsertionindex option specified, returns index @ should insert obj in order maintain sorted array:

  • if obj found , neither nsbinarysearchingfirstequal nor nsbinarysearchinglastequal specified, returns equal or 1 larger index matching object’s index.
  • [...]
  • if object not found, returns index of least greater object, or index @ end of array if object larger other elements.

special considerations: elements in array must have been sorted using comparator cmp. if array not sorted, result undefined.

your category method this:

- (void)ymy_insertsortedobject:(id)obj {     nsuinteger insertionindex = [self indexofobject:obj                                       insortedrange:nsmakerange(0, self.count)                                             options:nsbinarysearchinginsertionindex                                     usingcomparator:^(id obj1, id obj2){         return [obj1 compare:obj2];     }]; } 

edit: here's drop-in category can use: nsmutablearray+ymysorted.h


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -

matlab - How to equate a structure array to structure array -