0

I'm writing a small app for the iphone and I'm trying to write a function that will insert an NSMutableString into an NSArray in alphabetical order. Also I'll be writing a sort to sort the entire array as well. For both cases I'm wondering what the best way of comparing NSMutableStrings is. Is there a specific function I can use?

Thanks for your help.

4
  • Do you want to do a one time sort or sort while inserting? Commented Mar 15, 2012 at 4:16
  • Another issue is that you can't insert into an NSArray, you need an NSMutableArray. Commented Mar 15, 2012 at 4:20
  • @Jonathan: Did neither of the 6 answers help you? Commented Mar 15, 2012 at 6:56
  • Oh yes, most of them were actually helpful. I voted those ones up, but haven't really had time to try them out yet. Commented Mar 15, 2012 at 17:23

6 Answers 6

4

I think you're looking for

(NSComparisonResult)[aString compare: bString];

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/nsstring_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/compare:

You can use this or one of the related methods if you're doing insertion sort. However, if you want to do a one time sort of the NSMutableArray, you can use one of the NSMutableArray sorting methods such as sortUsingComparator:.

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableArray/sortUsingComparator:

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

Comments

2

If you look under "Identifying and comparing strings" in the NSString reference, you'll find several options. They do slightly different things, since you might want to compare strings in different ways (e.g. are numbers compared in lexical or numeric order?). The most basic is compare: — you can probably start there and choose a more complicated version as needed.

Comments

2

I think this should work for you. This is my answer which I have taken from the link:

Comparing text in UITextView?

SOLUTION-1: I have modified it here a bit to make it more easier for your case:

Let us assume String1 is one NSString.

 //Though this is a case sensitive comparison of string
 BOOL boolVal = [String1 isEqualToString:@"My Default Text"];

 //Here is how you can do case insensitive comparison of string:
 NSComparisonResult boolVal = [String1 compare:@"My Default Text" options:NSCaseInsensitiveSearch]; 

 if(boolVal == NSOrderedSame)
 {
     NSLog(@"Strings are same");
 }
 else
 {
     NSLog(@"Strings are Different");
 }

Here if boolVal is NSOrderedSame then you can say that strings are same else they are different.

SOLUTION-2: Also you don't find this easy, you can refer to Macmade's answer under the same link.

Hope this helps you.

Comments

2

For sorting array

NSArray *sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

Comments

1

try BOOL ans = [str1 isEqualToString:str2];

2 Comments

That's just about the only string comparison function I know. the only problem is I need to know which string is greater in the case that they're not equal.
Which string is greater ? What do you mean by greater ?
1

Try NSArray's sortedArrayUsingSelector: method:

NSArray * stringArray = [NSArray arrayWithObjects:@"dddsss", @"aada", @"bbb", nil];
[stringArray sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@", [stringArray sortedArrayUsingSelector:@selector(compare:)]);

Out put:

(
  aada,
  bbb,
  dddsss
)

What's more, you can use NSSortDescriptor to decide ASC or DESC order.

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.