I am trying to sort an array of NSObjects
(a object is of a class). The object class has several variables which I am wanting to use to sort the objects in the array, the variables I am using to sort are of type NSString
or NSInteger
, I am confident I am sorting NSString
s but for this question I am hoping to get help for sorting a NSInteger
.
This is the method I am using to sort the array of objects, It receives an NSMutableArray
of objects
- (NSMutableArray *)startSortingTheArray:(NSMutableArray *)unsortedArray
{
[unsortedArray sortUsingComparator:^ NSComparisonResult(SearchResultItem *d1, SearchResultItem *d2) {
NSInteger DoorID1 = d1.doorID;
NSInteger DoorID2 = d2.doorID;
NSComparisonResult result = [DoorID1 localizedCompare:DoorID2]; // Not sure how to sort these two integers
if (result == NSOrderedSame) {
NSString *handel1 = d1.handel;
NSString *handel2 = d2.handel;
result = [handel1 localizedCompare:handel2];
}
I am not sure how to compare the NSInteger, I have read that I could minus each number from itself.. if the number is 0 then they are equal or +1 / -1 etc they are not.. but not sure if thats the best way to approach this.
any help would be appreciated.
[unsortedArray sortUsingComparator:^ NSComparisonResult(SearchResultItem *d1, SearchResultItem *d2) {
NSInteger doorID1 = d1.doorID;
NSInteger doorID2 = d2.doorID;
if (doorID1 < doorID2)
return NSOrderedAscending;
if (doorID1 > doorID2)
return NSOrderedDescending;
return [d1.handel localizedCompare: d2.handel];
}];
as [aString localizedCompare:anotherString]
will return NSOrdered(Ascending|Descending|same), you can just return its result for the string case.