I thought I would pose a question I'm describing as a "Rosetta Stone". That is to say, I am posing a question, and would like to see answers given for a number of different languages so that someone moving from one language to another can see how some typical operation is done. I have always found these types of comparisons to be very educational when moving into a new language, and a fun quick way to see differences between languages without having to use them heavily for a while.
This particular question is, given an array of items in the typical collection appropriate to the language at hand, how do you sort them? Sample code with some description would be good.
I'll present the Objective C answer:
// Can only modify mutable arrays
NSMutableArray *sortArray = [NSMutableArray arrayWithArray:myArrayOfStuff];
Sort NSString ignoring case:
[sortArray sortUsingSelector: @selector(caseInsensitiveCompare:)];
Sort most foundation objects (would also work for NSString as well), assuming numbers are wrapped in NSNumber class:
[sortArray sortUsingSelector:@selector(compare:)];
Sort instances of your own classes:
[sortArray sortUsingSelector:@selector(myCompare:)];
For the last one, in your class you'd implement a comparison method like so:
// Class instance method to compare self with object "obj"
- (NSComparisonResult) myCompare:(myObject *)obj
{
NSComparisonResult retVal = NSOrderedSame;
if ( self < obj ) // by whatever rules make sense for your class of course
retVal = NSOrderedAscending;
else if ( self > obj )
retVal = NSOrderedDescending;
return retVal;
}