NSMutableArray 中的冒泡排序
这是我的问题:我有一个 plist
,其中包含一些对象。这些对象有一些键
,如“年龄”、“性别”、“人”等。如果这些键
的值
适合我的if 语句
,我将此对象添加到我的数组
中。
然后,当我的数组最终充满了一些合适的对象时,我想检查每个对象的另一个键 - “minimumCost”,其类型为 NSNumber,然后将对象从最便宜到最昂贵的对象排序。我不知道该怎么做。您能给我一些想法、代码或解决方案吗?
这是我的代码:
-(NSMutableArray*)creatingList:(NSMutableArray *)array
{
NSDictionary *mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"gifts" ofType:@"txt"]];
//---enumerate through the dictionary objects inside the rootDictionary
NSEnumerator *enumerator = [mainDictionary objectEnumerator];
id returnValue;
while ((returnValue = [enumerator nextObject]))
{
// Big if statement comes
if (([[returnValue valueForKey:@"sex"]isEqualToNumber:[NSNumber numberWithInt:sex]] ||
[[returnValue valueForKey:@"sex"]isEqualToNumber:[NSNumber numberWithInt:2]]) &&
[[returnValue valueForKey:@"person"]isEqualToString:person] &&
age >= [[returnValue valueForKey:@"minAge"] intValue] &&
age <= [[returnValue valueForKey:@"maxAge"] intValue])
{
[array addObject:[returnValue valueForKey:@"name"]];
}
}
for (int n = 0; n < [array count];n++)
{
// WHAT CODE SHOULD BE HERE ?
}
return array;
}
谢谢!
here is my question: I have a plist
with some objects in it. These objects have some keys
like "age", "sex" ,"person" and etc. If values
of these keys
are suitable for my if statement
, I add this object in my array
.
Then, when my array is finally full of some suitable objects I would like to check one more key of every object - "minimumCost" which type is NSNumber
and then sort objects from the cheapest to the most expensive objects. And I don't know how to do that. Could you give me some ideas, code or solutions please?
Here is my code:
-(NSMutableArray*)creatingList:(NSMutableArray *)array
{
NSDictionary *mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"gifts" ofType:@"txt"]];
//---enumerate through the dictionary objects inside the rootDictionary
NSEnumerator *enumerator = [mainDictionary objectEnumerator];
id returnValue;
while ((returnValue = [enumerator nextObject]))
{
// Big if statement comes
if (([[returnValue valueForKey:@"sex"]isEqualToNumber:[NSNumber numberWithInt:sex]] ||
[[returnValue valueForKey:@"sex"]isEqualToNumber:[NSNumber numberWithInt:2]]) &&
[[returnValue valueForKey:@"person"]isEqualToString:person] &&
age >= [[returnValue valueForKey:@"minAge"] intValue] &&
age <= [[returnValue valueForKey:@"maxAge"] intValue])
{
[array addObject:[returnValue valueForKey:@"name"]];
}
}
for (int n = 0; n < [array count];n++)
{
// WHAT CODE SHOULD BE HERE ?
}
return array;
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用内置的 NSArray 排序方法来完成此操作,而不是自行滚动。
在此处查看更多信息: http://www.cocoanetics.com/2009/ 03/nsarray-sorting-using-selectors/
You can use the built-in
NSArray
sorting methods to accomplish this, rather than rolling your own.See more here: http://www.cocoanetics.com/2009/03/nsarray-sorting-using-selectors/
请找到下面的代码,这是用 Objective-c 实现冒泡排序:
实现:
Please find the below code this is the implementation of Bubble sort with Objective-c :
Implementation:
这是对 NSMutableArray 整数进行未优化冒泡排序的最简单方法。
Here's the simplest way to do an unoptimized bubble sort on an NSMutableArray of integers.