我有一组自定义对象。这些对象表示二进制文件中的段。该属性loc
保存对象在文件中自己的位置,其中prev
保存“上一个”对象的位置。在这种情况下,“上一个”和“下一个”并不一定意味着对象在文件中相继出现。第一个对象有prev = 0
。最后一个对象没有以下对象将其位置保持为prev
。
我如何实现这种排序?对象的数量最初是未知的。
//My custom object
@interface MyObject : NSObject
@property (nonatomic, assign) NSInteger loc, prev;
@end
//In the implementation of some other class
NSMutableArray *array = [NSMutableArray new];
{// order should be 6
MyObject *obj = [MyObject new];
obj.loc = 3000;
obj.prev = 111;
[array addObject:obj];
}
{// order should be 2
MyObject *obj = [MyObject new];
obj.loc = 2000;
obj.prev = 222;
[array addObject:obj];
}
{// order should be 4
MyObject *obj = [MyObject new];
obj.loc = 333;
obj.prev = 4000;
[array addObject:obj];
}
{// order should be 1
MyObject *obj = [MyObject new];
obj.loc = 222;
obj.prev = 5000;
[array addObject:obj];
}
{// order should be 5
MyObject *obj = [MyObject new];
obj.loc = 111;
obj.prev = 333;
[array addObject:obj];
}
{// order should be 3
MyObject *obj = [MyObject new];
obj.loc = 4000;
obj.prev = 2000;
[array addObject:obj];
}
{// order should be 0
MyObject *obj = [MyObject new];
obj.loc = 5000;
obj.prev = 0;
[array addObject:obj];
}