59792

How can one sort an NSMutableArray of NSMutableArrays containing NSStrings?

Question:

Is there an easy way to sort an NSMutableArray of NSMutableArrays containing NSStrings?

I am sure that there must be an easy method for doing this but I can't seem to find it.

To Clarify I want to sort the first array alphabetically by the NSString at index 3 of the sub array.

Answer1:

Didn't see anyone actually answering this with other than "use this function, figure it out". So here is some actual code that you can use.

Put this category at the end of your .h file (or anywhere and import the .h where ever you need to sort)

@interface NSString (SortCompare) -(NSInteger)stringCompare:(NSString *)str2; @end

Put this in the .m file (or the .m of the .h you're importing)

@implementation NSString (SortCompare) -(NSInteger) stringCompare:(NSString *)str2 { return [(NSString *)self localizedCaseInsensitiveCompare:str2]; } @end

Now use the sort by calling sortUsingSelector: on the array<br /> NSMutableArray:

[myArray sortUsingSelector:@selector(stringCompare:)];

NSArray:

[myArray sortedArrayUsingSelector:@selector(stringCompare:)];

Answer2:

If your NSMutableArray only contains objects of type NSString, simply do:

[myMutableArray sortUsingSelector:@selector(compare:)];

Answer3:

As others have mentioned, NSArray doesn't have a compare: selector that allows it to sort itself, so you'll have to define it yourself. I would create a category on NSArray with a method called compare:(NSArray *)otherArray (or something that better describes what it does) and use that with sortUsingSelector:.

Depending on your needs you could possibly stuff all your strings into a big array when you need to sort them, and use that instead. It could be a little less code to write.

Answer4:

You can sort by key with:

NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:NO]; [yourArray sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];

Recommend

  • Direction of target annotation when outside of visible area
  • Badge value on “More” tab
  • access an alertView's calling view
  • Using extern @class in order to add a category?
  • “A GKScore must specify a leaderboard.”
  • Comparing user's facebook/twitter friends to site's users in Python/Django
  • Why can't UI components be accessed from a backgroundworker?
  • Firefox Extension - Monitor refresh and change of tab
  • how to save the state in userdefaults of accessory checkmark-iphone
  • C# - Serializing and deserializing static member
  • AES padding and writing the ciphertext to a disk file
  • output of program is not same as passed argument
  • Updating server-side rendering client-side
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to extract text from Word files using C#?
  • Eraser for UIBezierPath
  • How to limit post in wp_query
  • Upload files with Ajax and Jquery
  • Build own AppleScript numerical error handling
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • Python: how to group similar lists together in a list of lists?
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Change div Background jquery
  • How can I remove ASP.NET Designer.cs files?
  • Bitwise OR returns boolean when one of operands is nil
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?