NSMutableArray add object with order

前端 未结 3 850
猫巷女王i
猫巷女王i 2021-01-30 12:50

I have a NSMUtableArray which has elements, for example:

a,b,c,e

And I want to add an object d to behind c and befor

3条回答
  •  旧时难觅i
    2021-01-30 13:16

    I'd just add the new object at either end and sort the array again. If the array you're adding to is already sorted, the re-sort that moves one object is going to be about as quick as anything you'd implement yourself.

    NSMutableArray *things; // populated 
    id newObject;
    ...
    [things addObject:newObject atIndex:0];
    [things sortUsingSelector:@selector(compare:)];
    

提交回复
热议问题