Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort NSInteger using sortUsingComparator

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 NSStrings 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.

like image 873
HurkNburkS Avatar asked Aug 14 '12 00:08

HurkNburkS


3 Answers

[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.

like image 166
vikingosegundo Avatar answered Sep 27 '22 17:09

vikingosegundo


An NSInteger is not an object. It is just an integer (either an int or a long). So you can compare them exactly how you would compare two ints.

Classic C comparison functions allow you to do the x - y trick, but that's not appropriate here since you'd be returning an integer that doesn't fit into the NSComparisonResult enum. As such, you should probably just do something like

if (DoorID1 == DoorID2) {
    // compare secondary attribute
    NSString *handel1 = d1.handel;
    NSString *handel2 = d2.handel;
    return [handel1 localizedCompare:handel2];
}
else if (DoorID1 < DoorID2) return NSOrderedAscending;
else return NSOrderedDescending;

Incidentally, your sorting criteria can be represented with NSSortDescriptor instead, which may be clearer.

- (NSMutableArray *)startSortingTheArray:(NSMutableArray *)unsortedArray {
    NSSortDescriptor *primary = [NSSortDescriptor sortDescriptorWithKey:@"doorID" ascending:YES];
    NSSortDescriptor *secondary = [NSSortDescriptor sortDescriptorWithKey:@"handel" ascending:YES selector:@selector(localizedCompare:)];
    [unsortedArray sortUsingDescriptors:@[primary, secondary]];
}
like image 36
Lily Ballard Avatar answered Sep 27 '22 17:09

Lily Ballard


Another option:

NSComparisionResult res = [@(d1.doorID) compare:@(d2.doorID)];
return res == NSOrderedSame ? [d1.handel localizedCompare: d2.handel] : res;
like image 28
Dannie P Avatar answered Sep 27 '22 19:09

Dannie P