0

I have some Json input arrays that I want to sort. It is possible that some of the sort descriptors have null values. How can I protect against null value exceptions? Thanks.

What I have at present is:

//sort the group arrays
    NSSortDescriptor *sortDescriptor1a;
    NSSortDescriptor *sortDescriptor1b;
    sortDescriptor1a = [[NSSortDescriptor alloc] initWithKey:@"Min" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    sortDescriptor1b = [[NSSortDescriptor alloc] initWithKey:@"Line" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    NSArray *sortDescriptors1 = [NSArray arrayWithObjects:sortDescriptor1a, sortDescriptor1b, nil];
    NSArray *tempArray1 = [unsortedGroup1 sortedArrayUsingDescriptors:sortDescriptors1 ];
    currentGroup1 = [tempArray1 mutableCopy];

    NSSortDescriptor *sortDescriptor2a;
.....
ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • 1
    You may use a custom method instead of `caseInsensitiveCompare` and sort accordingly if value is `null` (NSOrderAscending/descending) – Larme Mar 02 '15 at 14:58
  • Hmm. Can you please show me roughly what a custom sort would look like.. (I can probably search for one, if you don't have something handy) – ICL1901 Mar 02 '15 at 15:02
  • 1
    Have a look at http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it for a very good example of custom sorts for arrays which `Larme` has mentioned – Popeye Mar 02 '15 at 15:05

1 Answers1

1

What is the sort behaviour that you desire in the case of nil values? The following will push nil values to the end of the array.

NSArray *sortedGroup1 = [unsortedGroup1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  NSComparisonResult result = NSOrderedSame;
  if (obj1.Min && obj2.Min) {
    result = [obj1.Min caseInsensitiveCompare:obj2.Min];
  } else if (obj1.Min) {
    result = NSOrderedDescending;
  } else if (obj2.Min) {
    result = NSOrderedAscending;
  }

  if (result == NSOrderedSame) {
    if (obj1.Line && obj2.Line) {
      result = [obj1.Line caseInsensitiveCompare:obj2.Line];
    } else if (obj1.Line) {
      result = NSOrderedDescending;
    } else if (obj2.Line) {
      result = NSOrderedAscending;
    }
  } 
  return result;
}];
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
  • Many thanks Ian. It's a great question -- what should happen. I have to get client input. – ICL1901 Mar 02 '15 at 15:46
  • If you want to specifically highlight these ones to a user and you're showing the data in a table, you'll probably want them to be first so they're at the top of the list. Alternatively, you could filter them out entirely and show them in a separate view. That's really more of an app design question, though. It's up to you (or your designer). – Ian MacDonald Mar 02 '15 at 15:51
  • They should come at the top of the list. – ICL1901 Mar 02 '15 at 15:56
  • Reverse the `Ascending` and `Descending`, then. – Ian MacDonald Mar 02 '15 at 16:11
  • D'oh. I should have seen that. – ICL1901 Mar 02 '15 at 16:12