xcode - 如何对二维多维 NSMutablearray 进行排序

标签 xcode cocoa sorting nsmutablearray multidimensional-array

首先我想对我糟糕的英语表示抱歉。希望您能理解。

我搜索了三天多的时间来寻找我的问题,但没有找到解决方案:( 我有一个包含 NSMutablearrays 的 NSMutablearray。

我从网络上获取数据。网络输出如下所示:

( ("Restaurant2" , "10" , "Hotel" , "Solitudestr" , "Germany" , "48.81155" , "9.10903"),("Club 2" , "14" , "Club" , "Weilimdorfer213" , "Germany" , "48.814" , "9.1311666666667"),("Thai Meat" , "22" , "Gastro" , "Weilimdorfer193" , " 70469 Stuttgart" , "48.813833333333" , "9.1328333333333") )

所以我在一个 NSMutablearray 里面有 3 个 NSMutablearray。我正在使用自定义单元格 View 在我的 Tableview 中显示数组。

我的问题是如何使用第一个值对三个内部数组进行排序。 (第一个值的值:Restaurant2、Club 2、Thai Meat)

所以第一个数组应该是:

("Club 2" , "14" , "Club" , "Weilimdorfer213" , "Germany" , "48.814" , "9.1311666666667")

第二个:

("Restaurant2" , "10" , "Hotel" , "Solitudestr" , "Germany" , "48.81155" , "9.10903")

第三:

("Thai Meat" , "22" , "Gastro" , "Weilimdorfer193" , " 70469 Stuttgart" , "48.813833333333" , "9.1328333333333")

我应该使用哪种排序函数以及如何使用?

在第二步中,我想获取给定坐标的距离并对到当前位置的距离进行排序。

我已经知道距离了。但如何排序呢?

非常感谢!

最佳答案

如果您要创建一个自定义对象来存储信息,而不是使用数组,这会容易得多。

看看 Dave DeLong 编写的这个示例在 this发布。

//ScoreRecord.h
@interface ScoreRecord : NSObject {
  NSString * label;
  NSUInteger score;
}
@property (nonatomic, retain) NSString * label;
@property (nonatomic) NSUInteger score;
@end

//ScoreRecord.m
#import "ScoreRecord.h"
@implementation ScoreRecord 
@synthesize label, score;

- (void) dealloc {
  [score release];
  [super dealloc];
}

@end

//elsewhere:
NSMutableArray * scores = [[NSMutableArray alloc] init];
ScoreRecord * first = [[ScoreRecord alloc] init];
[first setLabel:@"Label 1"];
[first setScore:1];
[scores addObject:first];
[first release];
//...etc for the rest of your scores

填充 scores 数组后,您现在可以执行以下操作:

//the "key" is the *name* of the @property as a string.  So you can also sort by @"label" if you'd like
NSSortDescriptor * sortByScore = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES];
[scores sortUsingDescriptors:[NSArray arrayWithObject:sortByScore]];

此后,您的 scores 数组将按分数升序排序。

看看Collections Programming TopicsNSSortDescriptor

关于xcode - 如何对二维多维 NSMutablearray 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9163095/

相关文章:

cocoa - 在 Cocoa 中获取 shell 脚本的输出

mysql - 高级 mysql 查询,它对结果集上的特定记录进行排序,而不考虑其默认排序?

ios - Xcode 10.1 - 模拟器崩溃试图在横向 iPhone 8 Plus、iPhone XR 和 iPhone XS Max 中呈现 SKStoreProductViewController

xcode - MacRuby + Interface Builder : How to display, 然后关闭,然后再次显示一个窗口

ios - 在导航 Controller 中如何实现添加按钮打开一个新的 View Controller

c++ - 将多重集排序为升序子序列,每个可用元素出现一次

java - 如何在java中对数组列表进行反向排序? (仅使用选择排序)

ios - 是否可以在我的应用程序中嵌入 wps

macos - 如何处理 OS X 10.9 和 10.10 之间的系统字体更改

objective-c - 连续的 'if' 语句