在 Objective C 中对二维数组进行排序

发布于 2024-09-17 17:55:06 字数 686 浏览 1 评论 0原文

我一直在目标 c 中对二维数组进行排序。我有一个由两个字符串数组组成的二维数组。

NSArray *totalRatings = [NSArray arrayWithObjects:namesArray, ratingsArray,nil];

我使用以下方法提取了值:

for (int i=0; i<2; i++) {
  for (int j=0; j<5; j++) {
     NSString *strings = [[totalRatings objectAtIndex:i] objectAtIndex:j];
     NSLog(@"i=%i, j=%i, = %@ \n",i,j,strings);
  }
}

首先,我想知道是否有更优雅的方法来处理totalRatings数组的长度。其次,totalRatings 数组包含的内容如下:

Person 1, 12
Person 2, 5
Person 3, 9
Person 4, 10
Person 7, 2

既然这些都是字符串,那么如何对它们进行排序呢?我正在寻找:

Person 1, 12
Person 4, 10
Person 3, 9
Person 2, 5
Person 7, 2

如果您能提供任何帮助,我将不胜感激。

I'm stuck with sorting 2D arrays in objective c. I have a 2D array which I made from two string arrays.

NSArray *totalRatings = [NSArray arrayWithObjects:namesArray,ratingsArray,nil];

I've pulled the values using:

for (int i=0; i<2; i++) {
  for (int j=0; j<5; j++) {
     NSString *strings = [[totalRatings objectAtIndex:i] objectAtIndex:j];
     NSLog(@"i=%i, j=%i, = %@ \n",i,j,strings);
  }
}

Firstly, I wonder whether there is a more elegant method for working with the length of the totalRatings array. Secondly, here is what the totalRatings array contains:

Person 1, 12
Person 2, 5
Person 3, 9
Person 4, 10
Person 7, 2

Since these are all strings, how can these be sorted? I'm looking for:

Person 1, 12
Person 4, 10
Person 3, 9
Person 2, 5
Person 7, 2

I'd appreciate any help you can provide.

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

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

发布评论

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

评论(1

烟酉 2024-09-24 17:55:06

您可能想使用字典来处理这些数据,而不是两个不同的数组。

为什么不将每个人的记录放入字典中,而不是现在所拥有的:

NSArray *totalRatings = [NSArray arrayWithObjects:
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Person 1", @"name", [NSNumber numberWithInt:12], @"rating", nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Person 2", @"name", [NSNumber numberWithInt:5], @"rating", nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Person 3", @"name", [NSNumber numberWithInt:9], @"rating", nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Person 4", @"name", [NSNumber numberWithInt:10], @"rating", nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Person 7", @"name", [NSNumber numberWithInt:2], @"rating", nil],
                             nil];
    NSArray *sortedArray = [totalRatings sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"rating" ascending:NO]]];

You may want to use a dictionary to deal with this data, instead of two distinct arrays.

Instead of what you have now, why don't you put the record for each person on a dictionary:

NSArray *totalRatings = [NSArray arrayWithObjects:
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Person 1", @"name", [NSNumber numberWithInt:12], @"rating", nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Person 2", @"name", [NSNumber numberWithInt:5], @"rating", nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Person 3", @"name", [NSNumber numberWithInt:9], @"rating", nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Person 4", @"name", [NSNumber numberWithInt:10], @"rating", nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Person 7", @"name", [NSNumber numberWithInt:2], @"rating", nil],
                             nil];
    NSArray *sortedArray = [totalRatings sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"rating" ascending:NO]]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文