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

发布于 2025-01-02 18:00:53 字数 1104 浏览 3 评论 0原文

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

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

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

( ("Restaurant2" , "10" , "酒店" , "Solitudestr" , "德国" , "48.81155" , "9.10903"),("俱乐部 2" , "14" , "俱乐部" , "Weilimdorfer213" , "德国" , "48.814" , "9.1311666666667"),("泰国肉" , "22" , “Gastro”、“Weilimdorfer193”、“70469 斯图加特”、“48.813833333333” ,“9.1328333333333”))

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

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

因此第一个数组应该是:

("俱乐部 2" , "14" , "俱乐部" , "Weilimdorfer213" , "德国" , "48.814" , “9.1311666666667”)

第二个:

("Restaurant2"、"10"、"酒店"、"Solitudestr"、"德国"、 “48.81155”,“9.10903”)

第三:

(“泰国肉”、“22”、“美食”、“Weilimdorfer193”、“70469 斯图加特", "48.813833333333", "9.1328333333333")

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

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

我已经得到了距离。但是怎么排序呢

非常感谢!

First i want to say sorry about my bad english. I hope you will unterstand.

i searched over three days for my problem and I found no solution :(
i have a NSMutablearray containing NSMutablearrays.

I get the data from the web. The weboutput looks like:

( ("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") )

So I have here 3 NSMutablearray's inside one NSMutablearray. I am showing the Array at my Tableview with a custom cellview.

My question is how can I sort the three inner arrays with first Value.
(Values of first Value: Restaurant2,Club 2,Thai Meat)

So the first array should be:

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

second:

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

third:

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

Which sortfunction should I use and how?

In the second step I want to get the distance of the given coordinates and sort the distance to the current position.

I got the distance, already. But how to sort this?

Thank you very much!

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

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

发布评论

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

评论(1

如若梦似彩虹 2025-01-09 18:00:53

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

看一下 Dave DeLong这篇文章

//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 数组将按分数升序排序。

看看 集合编程主题NSSortDescriptor

This would be much easier if you were to create a custom object to store the information, instead of using an array.

Take a look at this example written by Dave DeLong in this post.

//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

Once you've populated your scores array, you can now do:

//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]];

After this, your scores array will be sorted by the score ascending.

Take a look at Collections Programming Topics and NSSortDescriptor.

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