Search code examples
arraysswiftsortingnsset

Create array from NSSet in order


I am trying to create an array from an existing NSSet, but I can't find a method that creates the array in order. The only method I found was allObjects, but this method returns an array without a specified order (random indexes). Any ideas?


Solution

  • Use allObjects to get unsorted array, then use descriptor to do the sort stuff. For example:

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"type1.size" ascending:YES];
    NSArray *finalArray = [self.firstArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];