在 Objective C 中对二维数组进行排序
我一直在目标 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想使用字典来处理这些数据,而不是两个不同的数组。
为什么不将每个人的记录放入字典中,而不是现在所拥有的:
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: