[arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *first=[NSString stringWithFormat:@"%d%d",obj1.num1.intValue,obj1.num2.intValue];
NSString *second=[NSString stringWithFormat:@"%d%d",obj2.num1.intValue,obj2.num2.intValue];
return [first compare:second options:NSNumericSearch];
}];
Sort NSArray of NSobjects that contain NSNumber variables
-
18-10-2022 - |
Pregunta
I have an NSArray of Object Classes which consist of two textfields. I would like to sort these objects in ascending order, I have done this with NSDictionary objects before however I have now changed then to an Object Class that I have made so I dont really know how to compare the values to get the sorted array.
The object variables are NSNumbers but contain only number values, which I think will effect things.
This is how I was sorted the NSDictionary value with my old code.
NSArray *tempSortedItemsArray = [installArray sortedArrayUsingDescriptors:
@[[NSSortDescriptor
sortDescriptorWithKey:@"first" ascending:YES],
[NSSortDescriptor
sortDescriptorWithKey:@"second" ascending:YES]]];
sortedItemsArray = [tempSortedItemsArray mutableCopy];
tempSortedItemsArray = nil;
So if I have an array of object like this
(first / second)
2 1
0 0
1 0
1 1
2 2
2 0
3 0
if would sort like this
0 0
1 0
1 1
2 0
2 1
2 2
3 0
any help adjusting this for NSObject class with NSNumber variables first and second would be greatly appreicated.
Solución
Otros consejos
You can use the other sort methods of NSArray
such as:
sortedArrayUsingFunction:context:
sortedArrayUsingSelector:
sortedArrayUsingComparator:
Given an Object that works like this:
@interface Object : NSObject
@property (copy) NSNumber *first;
@property (copy) NSNumber *second;
+(Object *)objectWithWithFirst:(NSNumber *)first second:(NSNumber *)second;
@end
@implementation Object
+(Object *)objectWithWithFirst:(NSNumber *)first second:(NSNumber *)second {
Object *object = [[Object alloc] init];
object.first = first;
object.second = second;
return object;
}
-(NSString *)description {
return [NSString stringWithFormat:@"%@ %@", _first, _second];
}
@end
Your could sort with
NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(Object *obj1, Object *obj2) {
NSComparisonResult result = [obj2.second compare:obj2.second];
if (result == NSOrderedSame)
result = [obj1.first compare:obj2.first];
return result;
}];
Example:
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *array = @[
[Object objectWithWithFirst:@2 second:@1],
[Object objectWithWithFirst:@0 second:@0],
[Object objectWithWithFirst:@1 second:@0],
[Object objectWithWithFirst:@1 second:@1],
[Object objectWithWithFirst:@2 second:@2],
[Object objectWithWithFirst:@2 second:@0],
[Object objectWithWithFirst:@3 second:@0]
];
NSLog(@"%@", array);
NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(Object *obj1, Object *obj2) {
NSComparisonResult result = [obj2.second compare:obj2.second];
if (result == NSOrderedSame)
result = [obj1.first compare:obj2.first];
return result;
}];
NSLog(@"%@", sorted);
}
return 0;
}
Which will work perfectly on your sample data. Of course, you could put the comparator code directly into your Object class - cleaning thins up even more.
If your custom class has the properties first
and second
then the code you've posted will just work.
NSNumber
s already know how to compare themselves to other numbers. NSSortDesvriptor
s work with any object that implements the reading part of key-value coding. All NSObject
subclasses automatically do so for any property that follows ordinary conventions. @property
s follow ordinary conventions.