0

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. Commented Feb 19, 2013 at 6:21
  • My floorNames mutableArray containes floor names like GROUND, THIRD, FIRST, SECOND. How to sort this mutableArray to get GROUND, FIRST, SECOND ,THIRD. Commented Feb 19, 2013 at 6:27

2 Answers 2

4

Try this,

NSArray *arrFloorNames=[[NSArray alloc]initWithObjects:@"GROUND",@"THIRD",@"FIRST",@"SECOND", nil];
arrFloorNames = [arrFloorNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"%@",arrFloorNames);
Sign up to request clarification or add additional context in comments.

2 Comments

arrFloorNames = [arrFloorNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; NSLog(@"%@",arrFloorNames); By using this i am getting bad access in my application.
@user2043033 : How this code sorts as per your requirement. Kindly let me know.
0

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

Comments

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.