Sorting NSString values as if NSInteger using NSSortDescriptor

前端 未结 7 1497
执念已碎
执念已碎 2020-11-28 09:10

I have created a sort descriptor to sort a plist response coming from my server. This works well with sort key having values upto 9. With more than 10 items I see abrupt res

相关标签:
7条回答
  • 2020-11-28 09:44

    [list sortUsingSelector:@selector(localizedStandardCompare:)]; will sort the list in a "human" way (so "11" will come last, not between "1" and "2"). But if you really do want to treat these strings as numbers, you should make them number first!

    0 讨论(0)
  • 2020-11-28 09:51
    NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sort.intValue" ascending:YES];
    self.myList = [NSMutableArray arrayWithArray:[unsortedList sortedArrayUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]]];
    

    Sort based on the value of the integer.

    0 讨论(0)
  • 2020-11-28 09:56
    NSMutableArray *list = [NSMutableArray arrayWithObjects:@"11",@"2",@"3",@"1", nil];
    [list sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSInteger firstInteger = [obj1 integerValue];
        NSInteger secondInteger = [obj2 integerValue];
        if( firstInteger > secondInteger) return NSOrderedDescending;
        if( firstInteger == secondInteger) return NSOrderedSame;
        return NSOrderedAscending; // edited
    }];
    

    No guarantees about performance

    0 讨论(0)
  • 2020-11-28 09:56

    All the above methods need good knowledge of basics to implement; but for the newbies I suggest the simplest way – to use the native block method

    NSArray* sortedArr =[fetchResults sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    
        int aValue = [[a valueForKey:@"subgroupId"] intValue];
        int bValue = [[b valueForKey:@"subgroupId"] intValue];
    
        return aValue > bValue;
    }];
    
    0 讨论(0)
  • 2020-11-28 09:57

    You can do this by implementing a custom comparator block when creating your NSSortDescriptor:

    NSSortDescriptor *aSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"sort" ascending:YES comparator:^(id obj1, id obj2) {
    
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    self.myList = [NSMutableArray arrayWithArray:[unsortedList sortedArrayUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]]];
    

    See Apple documentation here

    0 讨论(0)
  • 2020-11-28 10:04

    input

    {
        "seats": [{
            "seatNumber": "1"
        }, {
            "seatNumber": "10"
        }, {
            "seatNumber": "2"
        }]
    }
    

    sort using [NSSortDescriptor sortDescriptorWithKey:@"seatNumber" ascending:YES selector:@selector(localizedStandardCompare:)]]. The key is to use localizedStandardCompare. This will solve you problem.

    NSArray *seats = [self.seats sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"seatNumber" ascending:YES selector:@selector(localizedStandardCompare:)]]];
    

    output

    {
        "seats": [{
            "seatNumber": "1"
        }, {
            "seatNumber": "2"
        }, {
            "seatNumber": "10"
        }]
    }
    

    documentation: https://developer.apple.com/documentation/foundation/nsstring/1409742-localizedstandardcompare

    0 讨论(0)
提交回复
热议问题