NSarray order

Discussion in 'iOS Programming' started by erdinc27, Mar 15, 2016.

  1. erdinc27 macrumors regular

    Jul 20, 2011
    i have NSArray and it holds NSDictionary objects. Something likes this
                imageDate = "12.02.2016 15:11";
                isSelected = 1;
    around 10 dictionaries like that. I want to order that array according to imageDate. Is it possible to order an array by comparing imageDate inside of a NSDictionary ?
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    You can sort an array using a comparison function that is based on any factors that you like.

    - (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context;
    NSInteger compareFunction(NSDictionary* first, NSDictionary* second, void* context)
    // Build NSDates and compare them
    NSString firstDateString = first[@"imageDate"];
    NSString secondDateString = second[@"imageDate"];
    // convert to NSDate here
    NSDate* firstDate = ...
    NSDate* secondDate = ...
    return [firstDate compare:secondDate];
    I think you need to use a date formatter to convert your imageDates to NSDate objects.
  3. erdinc27 thread starter macrumors regular

    Jul 20, 2011
    Thank you for your answer. Where should i implement that method ? I saw such kind of code for the first time.
  4. AxoNeuron macrumors 65816


    Apr 22, 2012
    The Left Coast
    It's pretty easy using NSSortDescriptor.

           let images = NSArray();   // add your image dictionaries to the array
            let sortDescriptor = NSSortDescriptor(key: "imageDate", ascending: true);
            let sortedImages = images.sortedArrayUsingDescriptors([sortDescriptor]);   // check to make sure it worked
        NSArray *images = @[];   // add your image dictionaries to the array
        NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"imageDate" ascending:YES];
        NSArray *sortedImages= [images sortedArrayUsingDescriptors:@[sorter]];   // check to make sure it worked.
    I can't remember if the NSSortDescriptor class can work directly with NSDate objects, I'm almost certain it can. But if it doesn't, you can always use the NSDate timeIntervalSince1970() method to get the UNIX timestamp for the date, convert it to an NSNumber, then set that nsnumber as an attribute of the image dictionaries and sort using that attribute instead. That will definitely work.

    If you are sorting many thousands of items (usually a bad idea just in principle to do this on the device, it should be done on the server), this task can become quite processor intensive, so you can throw it on to a background queue using dispatch_async.
  5. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Using sort descriptors is a good way to sort an array of dictionaries like this. Problem is that there is no NSDate inside the dictionary. the imageDate is a String. It's possible that a simple ascending sort of that string will work but it should probably be a numerical sort. If the dictionaries had NSDate objects in them that corresponded to the imageDate then that key/value pair could be used to sort the dictionaries.

    OP, sorting this array using the function is also an OK way to do this. To answer your question the sort function is a simple method. It goes in the class that is doing the sorting. So if that's a view controller it would be a method of the view controller. And then you sort the array like

    NSArray* sortedResult = [self.myArray sortedArrayUsingFunction:compareFunction context:nil];

Share This Page