使用数字字符串对数组进行排序

时间:2021-06-18 15:59:18

Hello I have an array of persons, and i am trying to sort them by age using a sort descriptor. The age field in a patient is a string so when calling:

你好,我有一个人的数组,我试图按年龄使用排序描述符对它们进行排序。患者的年龄字段是字符串,因此在致电时:

ageSorter = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
[personList sortUsingDescriptors:[NSArray arrayWithObject:ageSorter]];

It sorts them but 100 appears first because its is not using numericSearch in the compare options.

它对它们进行排序,但首先出现100,因为它在比较选项中没有使用numericSearch。

Is there a ways i can still sort with descriptor but maybe using a selector to change how to compare the strings?

有没有一种方法我仍然可以使用描述符排序,但可能使用选择器来更改如何比较字符串?

3 个解决方案

#1


1  

The finderSortWithLocale method (both these are taken from apple api):

finderSortWithLocale方法(这些都来自apple api):

int finderSortWithLocale(Person *person1, Person *person2, void *locale)
{
    static NSStringCompareOptions comparisonOptions = NSNumericSearch;

    NSRange string1Range = NSMakeRange(0, [string1 length]);
    NSString *age1 = person1.age;
    NSString *age2 = person2.age;
    return [age1 compare:age2
                    options:comparisonOptions
                    range:string1Range
                    locale:(NSLocale *)locale];
}

How to call this method (edited: call the function on array of Persons):

如何调用此方法(已编辑:调用人员数组上的函数):

    NSArray *sortedArray = [personList sortedArrayUsingFunction:finderSortWithLocale
                                         context:[NSLocale currentLocale]];

#2


1  

I also faced the same issue and found answer here. Instead of NSString comparison, do with your object property. i.e for age.

我也面临同样的问题,并在这里找到答案。而不是NSString比较,与您的对象属性。即年龄。

Example. : In ascending order :

例。 :按升序排列:

NSArray *sortedArray = [_arrayCaptureLeadList sortedArrayUsingComparator:^(Person *obj1, Person *obj2) {
            return [obj1.age compare:obj2.age options:NSNumericSearch];
        }];

NSMutableArray *filterResultArray = [NSMutableArray arrayWithArray:sortedArray];

In descending order :

按降序排列:

NSArray *sortedArray = [_arrayCaptureLeadList sortedArrayUsingComparator:^(Person *obj1, Person *obj2) {
            return [obj2.age compare:obj1.age options:NSNumericSearch];
        }];

NSMutableArray *filterResultArray = [NSMutableArray arrayWithArray:sortedArray];

I know this is very late to reply your question but may this will be helpful for others. ^_^

我知道回复你的问题已经很晚了,但这可能会对其他人有所帮助。 ^ _ ^

#3


0  

You could create a category on NSString that adds a method numericCompare: and which calls [self compare:otherString options:NSNumericSearch]. Another option is to convert the age field into a NSNumber instead of a NSString. Yet another option involves a NSComparator block and sortUsingComparator.

您可以在NSString上创建一个类别,它添加一个方法numericCompare:并调用[self compare:otherString options:NSNumericSearch]。另一种选择是将年龄字段转换为NSNumber而不是NSString。另一个选项涉及NSComparator块和sortUsingComparator。

#1


1  

The finderSortWithLocale method (both these are taken from apple api):

finderSortWithLocale方法(这些都来自apple api):

int finderSortWithLocale(Person *person1, Person *person2, void *locale)
{
    static NSStringCompareOptions comparisonOptions = NSNumericSearch;

    NSRange string1Range = NSMakeRange(0, [string1 length]);
    NSString *age1 = person1.age;
    NSString *age2 = person2.age;
    return [age1 compare:age2
                    options:comparisonOptions
                    range:string1Range
                    locale:(NSLocale *)locale];
}

How to call this method (edited: call the function on array of Persons):

如何调用此方法(已编辑:调用人员数组上的函数):

    NSArray *sortedArray = [personList sortedArrayUsingFunction:finderSortWithLocale
                                         context:[NSLocale currentLocale]];

#2


1  

I also faced the same issue and found answer here. Instead of NSString comparison, do with your object property. i.e for age.

我也面临同样的问题,并在这里找到答案。而不是NSString比较,与您的对象属性。即年龄。

Example. : In ascending order :

例。 :按升序排列:

NSArray *sortedArray = [_arrayCaptureLeadList sortedArrayUsingComparator:^(Person *obj1, Person *obj2) {
            return [obj1.age compare:obj2.age options:NSNumericSearch];
        }];

NSMutableArray *filterResultArray = [NSMutableArray arrayWithArray:sortedArray];

In descending order :

按降序排列:

NSArray *sortedArray = [_arrayCaptureLeadList sortedArrayUsingComparator:^(Person *obj1, Person *obj2) {
            return [obj2.age compare:obj1.age options:NSNumericSearch];
        }];

NSMutableArray *filterResultArray = [NSMutableArray arrayWithArray:sortedArray];

I know this is very late to reply your question but may this will be helpful for others. ^_^

我知道回复你的问题已经很晚了,但这可能会对其他人有所帮助。 ^ _ ^

#3


0  

You could create a category on NSString that adds a method numericCompare: and which calls [self compare:otherString options:NSNumericSearch]. Another option is to convert the age field into a NSNumber instead of a NSString. Yet another option involves a NSComparator block and sortUsingComparator.

您可以在NSString上创建一个类别,它添加一个方法numericCompare:并调用[self compare:otherString options:NSNumericSearch]。另一种选择是将年龄字段转换为NSNumber而不是NSString。另一个选项涉及NSComparator块和sortUsingComparator。