0
votes

I am getting floor names and storing in _floorNames (NSMutableArray) by using

_floorNames=[conferenceHall floorDetails:office];


NSLog(@"floor names=%@",_floorNames);

This will print floor names like this.

 GROUND,
 THIRD,
 FIRST,
 SECOND

Now i want to sort that floor names NsMutableArray as

GROUND,
FIRST,
SECOND,
THIRD

I have used

 NSSortDescriptor *aa=[[NSSortDescriptor alloc]initWithKey:floor ascending:YES                                                           selector:@selector(localizedCaseInsensitiveCompare:)];

 NSArray *descriptors = [NSArray arrayWithObjects:aa, nil]; 
   a = [array sortedArrayUsingDescriptors:descriptors];

But it will crash my application. Any suggestions ...

2
Suggestions: #1: don't use the Xcode tag, it's inappropriate. #2: format your code, it's impossible to read. #3: ask a real question - I couldn't deduce what you expect to happen and what actually happens. - user529758
My floorNames mutableArray containes floor names like GROUND, THIRD, FIRST, SECOND. How to sort this mutableArray to get GROUND, FIRST, SECOND ,THIRD. - user2043033

2 Answers

4
votes

Try this,

NSArray *arrFloorNames=[[NSArray alloc]initWithObjects:@"GROUND",@"THIRD",@"FIRST",@"SECOND", nil];
arrFloorNames = [arrFloorNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"%@",arrFloorNames);
0
votes

What you want is you never get!!!

"GROUND, THIRD, FIRST, SECOND" to "GROUND, FIRST, SECOND ,THIRD" is what you want.

No algorithm can sort these strings based on their internal representation in numerals. This is something like, 0..1..2..3. You want to sort these integers written in English. Sorting can be done alphabetically so your result will be as First, Groung, Second, Third.

How to achieve?

You need to create an object of dictionary, with mapped value of Third to 3, second to 2, ground to 0. Then sort those numerical values. And then get Keys based on values(0,1,2etc).