I have an array of "room" objects, each of these has a property called "room.descritpion" that is an NSString containing a mix of alphanumerical characters. These rooms need to be sorted in an array using the "room.description" property with ascending alphabetical order and then by number order ascending. I can get the alphabetical sorting done easily but I am struggling with how to incorporate the second sort which will be based on the number following the letter e.g. L21 L19 A34 A12 should be ordered in the array as A12, A34, L19, L21 and so on. The priority of the search should be letter first and then by number so a room with only numbers comes at the end of the array. The letters and numbers are not always in the same order i.e. the letter is not always first. I have read the documentation thoroughly but cannot seem to find the way to approach this. I am happy to use blocks if it makes the process easier. Any help would be greatly appreciated.
Asked
Active
Viewed 663 times
2 Answers
2
NSArray *unorderedTitles = [NSArray arrayWithObjects:@"L21",@"L19",@"A34",@"A12",@"A1", nil];
NSArray *orderedTitles = [unorderedTitles sortedArrayUsingSelector:@selector(localizedStandardCompare:)];
NSLog(@"orderedTitles %@",orderedTitles);

Community
- 1
- 1

Muhammad Adnan
- 2,668
- 15
- 27
-
Gave this a go but it generated an exception *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Rooms localizedStandardCompare:]: unrecognized selector sent to instance 0x79fc8310' I am guessing this is because the objects in the Array are room objects that have a string property that describes what they are. I cannot organise via strings directly. Is there anyway to reference the room.description property using this method rather than the room object which is what I presume is happening here. – jon Dec 31 '15 at 14:03
-
whoever down voted should give a reason.That will help me to learn something which i am doing wrong. Any criticism is welcome. – Muhammad Adnan Jan 01 '16 at 04:29
1
This
var arr = ["A6", "A2", "3", "B4", "L8", "4", "B7"]
let sortedArr = arr.sort({String($0) < String($1)})
print(sortedArr)
Will print out
["3", "4", "A2", "A6", "B4", "B7", "L8"]

Rashwan L
- 38,237
- 7
- 103
- 107