Objective-c 按最后一个对象对数组进行排序

发布于 2025-01-07 06:29:02 字数 501 浏览 3 评论 0原文

我对数组排序有问题。我已经构建了排序方法,但它无法正常工作。我的意思是最终表应该按最后一个元素排序,使用最后一个元素降序排列。

我的方法:

static NSInteger order (id a, id b, void* context) 
        {
        NSNumber* catA = [a lastObject];
        NSNumber* catB = [b lastObject];
        return [ catB compare: catA];
        }

并通过以下方式调用它:

[ array sortUsingFunction:order context:NULL];

我的数组是这样的:

{1,9}
{1,6}
{1,5}
{2,2}
{0,18}
{12, 10}
{9,1}

问题出在哪里?

I have problem with sort array of arrays. I already build method to sort but it not work properly. I mean that final table should be sorted by last element, descending using the last element.

My method:

static NSInteger order (id a, id b, void* context) 
        {
        NSNumber* catA = [a lastObject];
        NSNumber* catB = [b lastObject];
        return [ catB compare: catA];
        }

And call it by:

[ array sortUsingFunction:order context:NULL];

And my array is sort that:

{1,9}
{1,6}
{1,5}
{2,2}
{0,18}
{12, 10}
{9,1}

Where is problem?

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

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

发布评论

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

评论(1

雪花飘飘的天空 2025-01-14 06:29:02

排序后,您并没有确切地说出数组有什么问题。我看到两个可能的问题。

  1. 正如 Eimantas 在他的评论中所说,您正在按相反的顺序(从最高到最低)对数组进行排序。如果您想从最低到最高排序,则需要说 return [catAcompare:catB]

  2. 看起来 catAcatB 的元素是字符串,而不是数字,因此您将它们作为字符串进行排序。字符串“10”小于字符串“9”,但数字 10 大于数字 9。即使您将元素转换为 NSNumber,这也不会更改该元素的类型底层对象,仍然是 NSString

您可以通过这种方式对它们进行数字排序:

[array sortUsingComparator:^(id a, id b) {
    return [[a lastObject] intValue] - [[b lastObject] intValue];
}]

但在对数组进行排序之前,最好将字符串转换为数字对象:

for (NSMutableArray *element in array) {
    [element replaceObjectAtIndex:(element.count - 1)
        withObject:[NSNumber numberWithInt:[element.lastObject intValue]]];
}

[array sortUsingComparator:^(id a, id b) {
    return [[a lastObject] intValue] - [[b lastObject] intValue];
}]

You don't say exactly what is wrong with the array after you sort it. I see two possible problems.

  1. As Eimantas says in his comment, you are sorting the array in reverse order (highest to lowest). If you want to sort lowest to highest, you need to say return [catA compare:catB].

  2. It looks like the elements of catA and catB are strings, not numbers, so you are sorting them as strings. The string '10' is less than the string '9', but the number 10 is greater than the number 9. Even though you are casting the elements to NSNumber, that does not change the type of the underlying object, which is still NSString.

You can sort them as numbers this way:

[array sortUsingComparator:^(id a, id b) {
    return [[a lastObject] intValue] - [[b lastObject] intValue];
}]

But it might be better to convert the strings to number objects before sorting the array:

for (NSMutableArray *element in array) {
    [element replaceObjectAtIndex:(element.count - 1)
        withObject:[NSNumber numberWithInt:[element.lastObject intValue]]];
}

[array sortUsingComparator:^(id a, id b) {
    return [[a lastObject] intValue] - [[b lastObject] intValue];
}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文