Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Code that Alphabetically organises

Hi all , I recently copied a program which made a picker wheel app . I wanted to write a line or two of code that will alphabetically organise the items in the array or in the wheel, as it would take to long to write them all in order.

Is there any code that would do this as I can't find any so far in documents.

Many thanks

3 Answers

Hi Jon,

Because you are learning Swift, you are probably better off building those skills and not worrying too much about Objective-C, at least for now. If you become a professional iOS developer, then learning Objective-C is more worthwhile because it will allow you to understand legacy code. However, it is possible to use both languages within the same project. Here is a different approach to solving your problem, this time using Swift:

var fruit = ["bananas", "oranges", "peaches", "apples"]
var organizedFruit = sorted( fruit, < )

Hi Jon,

If you are using Objective-C and you need to alphabetize an array of String objects, you can use the sortUsingSelector method:

int main( int argc, const char * argv[] )
{
    NSMutableArray * colors = [[NSMutableArray alloc]initWithObjects:
                               @"green", @"blue", @"yellow", @"red", @"brown", @"pink", nil];

    [colors sortUsingSelector: @selector(caseInsensitiveCompare:)];

    for ( NSString * color in colors )
    {
        NSLog( @"%@\n", color );
    }

    return 0;
}

thank you Zack , sorry I should have said im learning swift but im guessing objective-c will work mixed with swift in xcode ?

//so for example if I had a var fruit = [bananas, oranges, peaches, apples]

// putting your code 1st will organise that ? [fruit sortUsingSelector: @selector(caseInsensitiveCompare:)

many thank Jon