5

I have an array containing custom objects with a property named seat. Seat can have values 1A, 1D , 1C , 1k , 2A, 2k, 2D, 2C.

Now these can be arranged in any order, and I want to sort them according to class, however the sorting only accounts for the seats numeric value and not A, C, D,or K.

I want the order to be 1A,1C,1D,1K and so on.

This is what I have implemented in the SeatDO object:

-(NSComparisonResult) compareBySeatNumber:(SeatDO*)other {
    NSComparisonResult result = NSOrderedSame;
    NSInteger seatNumber = [self.seat integerValue];
    NSInteger otherSeatNumber = [other.seat integerValue];

    if (seatNumber > otherSeatNumber) {
        result = NSOrderedDescending;
    } else if (seatNumber < otherSeatNumber) {
        result = NSOrderedAscending;
    }
    return result;
}

How do I make it consider the letters as well..?

3
  • Is the 'seat' implemented as enum? If not, what type of object are the values of 'seat'? Commented Jun 18, 2013 at 7:57
  • @makaron seats are nsstring Commented Jun 18, 2013 at 8:02
  • 3
    Well, then Gabriele's answer should work ) Commented Jun 18, 2013 at 8:02

3 Answers 3

7

Assuming that you convert the seat numbers to NSStrings

NSArray *sortedSeats = [seats sortedArrayUsingSelector:@selector(localizedStandardCompare:)]

should do the trick. Sorting strings will naturally follow the sort order you need.

Otherwise you could just use strings during the comparison with

[seats sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [[obj1 stringValue] localizedStandardCompare:[obj2 stringValue]];
}];

I assumed that stringValue is available for you custom object. If not, simply replace it with anything that will return a NSString description of your instances.

NOTE

As suggested by Alladinian, you want to use localizedStandardCompare: as opposed to caseInsensitiveCompare:, in order to the get the proper lexicographic order.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Gabriele (thanx for the fairplay by the way, this is what makes this community so awesome). Since your answer have been selected as correct you might want to fix a small typo: sortedArrayUsingSelect: should be sortedArrayUsingSelector:
3

Use localizedStandardCompare: as the selector (Finder-like sorting)

[seats sortedArrayUsingSelector:@selector(localizedStandardCompare:)]

While caseInsensitiveCompare: might seem like correct, if you add a @"10D" or @"01C" seat it would appear in front of all others...

Comments

-2

if you're using an NSArray you can sort it using sortedArrayUsingSelector:(SEL)comparator and it will return you another array sorted

NSArray *arraySorted = [myArray sortedArrayUsingSelector:@selector(compareBySeatNumber:)];

3 Comments

Seems like you didn't get my question.. I know how to get the sorted array, but the sorting mechanism does not account for the letters in the seat.. only the numerics.
The user wanted to know how to take in account also the letter when sorting, not how to sort an array
Moreover sortedArrayUsingSelector will send the selector the each object held by the array, which of course do not respond to compareBySeatNumber:. The above code would crash the app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.