简体   繁体   中英

Sort an array for an objects double property

I need to reorder an NSMutableArray using a property of the objects inside of it.

I did this for dates in a previous project with dates but this method won't accept doubles:

NSUInteger rowIndex = [self.alist indexOfObject:assignment inSortedRange:NSMakeRange(0, self.alist.count) options:NSBinarySearchingInsertionIndex usingComparator:^(AssignmentInfo *obj1, AssignmentInfo *obj2) {

    NSDate *date1 = obj1.dateTime;
    NSDate *date2 = obj2.dateTime;

    if ([date1 compare:date2] == NSOrderedAscending) {
        return NSOrderedAscending;
    }
    if ([date1 compare:date2] == NSOrderedDescending) {
        return NSOrderedDescending;
    }
    return NSOrderedSame;

}];

So no I need a way to reorder the array self.communitiesArray for the double value of the community object inside the array. self.community.distanceFromUser which is the double.

Using some of the answers below I am getting an error on:

NSUInteger rowIndex = [self.communitiesArray indexOfObject:community inSortedRange:NSMakeRange(0, self.communitiesArray.count) options:NSBinarySearchingInsertionIndex usingComparator:^(Community *obj1, Community *obj2) {

Error says : Incompatible block pointer types sending 'void (^)(Community *_ strong, Community * _strong)' to parameter of type 'NSComparator' (aka 'NSComparisonResult (^)(__strong id, __strong id)')

Since double s are primitives, you do not need to use the compare: method on them: comparing them with the regular operators should be sufficient. Your comparator block would look as follows:

double d1 = obj1.doubleValue;
double d2 = obj2.doubleValue;
if (d1 < d2) {
    return NSOrderedAscending;
}
if (d1 > d2) {
    return NSOrderedDescending;
}
return NSOrderedSame;

An even better approach would be using NSSortdescriptor s: they let you sort your objects based on the properties they expose without writing any additional code:

NSSortDescriptor *dblDescr= [[NSSortDescriptor alloc] initWithKey:@"doubleValue" ascending:YES];
NSArray *sortedArray = [myData sortedArrayUsingDescriptors:@[dblDescr]];

您可以使用NSSortDescriptor对数组进行排序。

[array sortUsingDescriptors:@[[[NSSortDescriptor alloc] initWithKey:@"community.distanceFromUser" ascending:YES]]];

Just compare the double s directly, ie, in the comparator:

double a = obj1.distanceFromUser;
double b = obj2.distanceFromUser;
if (a < b) {
    return NSOrderedAscending;
} else if (a > b) {
    return NSOrderedDescending;
} else {
    return NSOrderedSame;
}

As others said, double is a primitive type so you can use the comparison operators to compare them

NSUInteger rowIndex =
     [self.communitiesArray indexOfObject:community
                            inSortedRange:NSMakeRange(0, self.communitiesArray.count)
                                  options:NSBinarySearchingInsertionIndex
                          usingComparator:^NSComparisonResult (Community *obj1, Community *obj2) {
       double distance1 = obj1.distanceFromUser;
       double distance2 = obj2.distanceFromUser;
       if (distance1 < distance2)
           return NSOrderedAscending;
       if (distance1 < distance2)
           return NSOrderedDescending;
       return NSOrderedSame;
}

Notice the block signature: you were passing ^(Community *obj1, Community *obj2) , which is interpreted as ^void (Community *obj1, Community *obj2) , whereas you have to include the correct return type, ie ^NSComparisonResult (Community *obj1, Community *obj2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM