按 NSDictionary 值对 NSArray 进行排序

发布于 2024-09-11 00:13:34 字数 1708 浏览 3 评论 0原文

我正在尝试对一个看起来像这样的数组进行排序: (请忽略这些人已经过了任何生活年龄的事实!我只需要大量的数字)

NSDictionary *person1 = [NSDictionary dictionaryWithObjectsAndKeys:@"sam",@"name",@"28.00",@"age",nil];
NSDictionary *person2 = [NSDictionary dictionaryWithObjectsAndKeys:@"cody",@"name",@"100.00",@"age",nil];
NSDictionary *person3 = [NSDictionary dictionaryWithObjectsAndKeys:@"marvin",@"name",@"299.00",@"age",nil];
NSDictionary *person4 = [NSDictionary dictionaryWithObjectsAndKeys:@"billy",@"name",@"0.0",@"age",nil];
NSDictionary *person5 = [NSDictionary dictionaryWithObjectsAndKeys:@"tammy",@"name",@"54.00",@"age",nil];

NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:person1,person2,person3,person4,person5,nil];

// before sort
NSLog(@"%@",arr);

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

// after sort
NSLog(@"%@",arr);

现在在排序之前输出将是

2010-07-21 10:46:31.898 Sorting[70673:207] (
    {
    age = "28.00";
    name = sam;
},
    {
    age = "100.00";
    name = cody;
},
    {
    age = "299.00";
    name = marvin;
},
    {
    age = "0.0";
    name = billy;
},
    {
    age = "54.00";
    name = tammy;
}

:)

和排序之后

2010-07-21 10:46:31.900 Sorting[70673:207] (
    {
    age = "0.0";
    name = billy;
},
    {
    age = "100.00";
    name = cody;
},
    {
    age = "28.00";
    name = sam;
},
    {
    age = "299.00";
    name = marvin;
},
    {
    age = "54.00";
    name = tammy;
}

:)

正如你所看到的,它确实对它进行了排序,但根据我的理解,它正在排序通过字符串。我已经尝试过,但在尝试编写一种方法来为我解决这个问题几天失败后,我仍然不知所措。实现此目的的最佳方法是什么,以便按数值排序?

I'm trying to sort an array that would look something like this:
(please ignore the fact these people are well past any living age! I just needed large numbers)

NSDictionary *person1 = [NSDictionary dictionaryWithObjectsAndKeys:@"sam",@"name",@"28.00",@"age",nil];
NSDictionary *person2 = [NSDictionary dictionaryWithObjectsAndKeys:@"cody",@"name",@"100.00",@"age",nil];
NSDictionary *person3 = [NSDictionary dictionaryWithObjectsAndKeys:@"marvin",@"name",@"299.00",@"age",nil];
NSDictionary *person4 = [NSDictionary dictionaryWithObjectsAndKeys:@"billy",@"name",@"0.0",@"age",nil];
NSDictionary *person5 = [NSDictionary dictionaryWithObjectsAndKeys:@"tammy",@"name",@"54.00",@"age",nil];

NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:person1,person2,person3,person4,person5,nil];

// before sort
NSLog(@"%@",arr);

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

// after sort
NSLog(@"%@",arr);

Now before sort the output would be:

2010-07-21 10:46:31.898 Sorting[70673:207] (
    {
    age = "28.00";
    name = sam;
},
    {
    age = "100.00";
    name = cody;
},
    {
    age = "299.00";
    name = marvin;
},
    {
    age = "0.0";
    name = billy;
},
    {
    age = "54.00";
    name = tammy;
}

)

and after the sort:

2010-07-21 10:46:31.900 Sorting[70673:207] (
    {
    age = "0.0";
    name = billy;
},
    {
    age = "100.00";
    name = cody;
},
    {
    age = "28.00";
    name = sam;
},
    {
    age = "299.00";
    name = marvin;
},
    {
    age = "54.00";
    name = tammy;
}

)

As you can see it does sort it, but from my understanding it's sorting by string. I've tried but after a few days of failure of trying to write a method that would sort this for me im still at a loss. What would be the best approach and accomplishing this so it sorts by a numeric value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀お生予夺 2024-09-18 00:13:34

尽管我对这里字符串的使用提出疑问,但处理该数据的最简单方法是:

[array sortedArrayUsingComparator:^(NSDictionary *item1, NSDictionary *item2) {
    NSString *age1 = [item1 objectForKey:@"age"];
    NSString *age2 = [item2 objectForKey:@"age"];
    return [age1 compare:age2 options:NSNumericSearch];
}];

或者,使用 Objective-C 最新的下标功能:

[array sortedArrayUsingComparator:^(NSDictionary *item1, NSDictionary *item2) {
    return [item1[@"age"] compare:item2[@"age"] options:NSNumericSearch];
}];

Although I question the use of strings here, the simplest way to work with that data with be:

[array sortedArrayUsingComparator:^(NSDictionary *item1, NSDictionary *item2) {
    NSString *age1 = [item1 objectForKey:@"age"];
    NSString *age2 = [item2 objectForKey:@"age"];
    return [age1 compare:age2 options:NSNumericSearch];
}];

Or, using Objective-C's more recent subscripting features:

[array sortedArrayUsingComparator:^(NSDictionary *item1, NSDictionary *item2) {
    return [item1[@"age"] compare:item2[@"age"] options:NSNumericSearch];
}];
夏了南城 2024-09-18 00:13:34

最简单的方法是将年龄存储为数字而不是字符串。

Easiest would be storing the age as a number instead of a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文