排序数组指定索引 IOS

sort array specified indexes IOS

我想根据名为 id 的对象过滤 NSARRAY,我有一组我想要过滤的特定 ID,并希望成为 NSARRAY 中的第一个。

我将以下 ID 存储为 NSNUMEBR

         NSNumber *A = [NSNumber numberWithInt:1122];
         NSNumber *B = [NSNumber numberWithInt:1345];
         NSNumber *C = [NSNumber numberWithInt:1667];
         NSNumber *D = [NSNumber numberWithInt:1223];
         NSNumber *E = [NSNumber numberWithInt:1213];
         NSNumber *F = [NSNumber numberWithInt:1123];
         NSNumber *G = [NSNumber numberWithInt:1555];
         NSNumber *H = [NSNumber numberWithInt:1666];
         NSNumber *I = [NSNumber numberWithInt:1567];

这些是我要过滤的id集合,希望在我的NSARRAY中排在第一位(可以是NSMutableArray进行操作)

编辑 1:

NSARRAY 基本上是获取 id 对象作为

Ids = [dict valueForKey:@"id"];

选择性 ID 存储在 NSNUMBER A 到 I

不清楚您的问题,如反对票和评论所示。但是让我们看看我们是否能提供帮助。我认为以下伪代码算法是您所要求的:

MutableArray frontItems, rearItems;

for every item in sourceArray
   if item["id"] is in the collection of specific IDs
   then add item to end of frontItems
   else add item to end of rearItems

add rearItems to end of frontItems to give result

在Objective-C中写下,我想你已经得到了你想要的。

HTH

//创建包含所有项的数组:A->I

NSNumber *A = [NSNumber numberWithInt:1122];
NSNumber *B = [NSNumber numberWithInt:1345];
NSNumber *C = [NSNumber numberWithInt:1667];
NSNumber *D = [NSNumber numberWithInt:1223];
NSNumber *E = [NSNumber numberWithInt:1213];
NSNumber *F = [NSNumber numberWithInt:1123];
NSNumber *G = [NSNumber numberWithInt:1555];
NSNumber *H = [NSNumber numberWithInt:1666];
NSNumber *I = [NSNumber numberWithInt:1567];

NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:A,B,C,...,I, nil];

for (NSNumber *idx in arr) {
    // To do
}