NSMutableArray 中的冒泡排序

发布于 2024-12-26 17:45:32 字数 1561 浏览 5 评论 0原文

这是我的问题:我有一个 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 技术交流群。

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

发布评论

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

评论(3

我爱人 2025-01-02 17:45:32

您可以使用内置的 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/

羅雙樹 2025-01-02 17:45:32

请找到下面的代码,这是用 Objective-c 实现冒泡排序:

- (void)viewDidLoad {
   [super viewDidLoad];

    NSMutableArray *unsortedArray = [[NSMutableArray alloc]initWithObjects:@"4",@"7",@"3",@"2",@"10",@"8",nil];
    [self bubbleSort:unsortedArray];
  }

实现:

- (NSArray *)bubbleSort:(NSMutableArray *)sortedArray
{
   long count = sortedArray.count;

   bool swapped = YES;

   while (swapped)
   {
   swapped = NO;

      for (int i = 1; i < count; i++)
      {
          int x = [sortedArray[i-1] intValue];
          int y = [sortedArray[i] intValue];

          if (x > y)
          {
               [sortedArray exchangeObjectAtIndex:(i-1) withObjectAtIndex:i];
               swapped = YES;
          }
      }
   }
   return sortedArray;
}

Please find the below code this is the implementation of Bubble sort with Objective-c :

- (void)viewDidLoad {
   [super viewDidLoad];

    NSMutableArray *unsortedArray = [[NSMutableArray alloc]initWithObjects:@"4",@"7",@"3",@"2",@"10",@"8",nil];
    [self bubbleSort:unsortedArray];
  }

Implementation:

- (NSArray *)bubbleSort:(NSMutableArray *)sortedArray
{
   long count = sortedArray.count;

   bool swapped = YES;

   while (swapped)
   {
   swapped = NO;

      for (int i = 1; i < count; i++)
      {
          int x = [sortedArray[i-1] intValue];
          int y = [sortedArray[i] intValue];

          if (x > y)
          {
               [sortedArray exchangeObjectAtIndex:(i-1) withObjectAtIndex:i];
               swapped = YES;
          }
      }
   }
   return sortedArray;
}
地狱即天堂 2025-01-02 17:45:32

这是对 NSMutableArray 整数进行未优化冒泡排序的最简单方法。

  NSMutableArray <NSNumber *>*foo = [NSMutableArray arrayWithArray:@[@9,@8,@7,@6,@5,@4]];
  for (int i = 0; i < [foo count]; i++) {
    for (int j = 0; j < [foo count] - 1; j++) {
      if (foo[j + 1].integerValue < foo[j].integerValue) {
        NSInteger a = foo[j].integerValue;
        foo[j] = foo[j + 1];
        foo[j + 1] = @(a);
      }
    }
  }

Here's the simplest way to do an unoptimized bubble sort on an NSMutableArray of integers.

  NSMutableArray <NSNumber *>*foo = [NSMutableArray arrayWithArray:@[@9,@8,@7,@6,@5,@4]];
  for (int i = 0; i < [foo count]; i++) {
    for (int j = 0; j < [foo count] - 1; j++) {
      if (foo[j + 1].integerValue < foo[j].integerValue) {
        NSInteger a = foo[j].integerValue;
        foo[j] = foo[j + 1];
        foo[j + 1] = @(a);
      }
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文