objective-cnsmutablearrayplistxcode4.2bubble-sort

Bubble sort in NSMutableArray


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!


Solution

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