Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 11 / 68

Sorting an array

This lesson covers sorting an array with sortedArrayUsingSelector in Objective-C, with the aim of understanding an operation used constantly in practice.

sortedArrayUsingSelector: sorts an array. Passing the compare: selector specifies how elements are compared.

The sample code gets a new, ascending-order array with [nums sortedArrayUsingSelector:@selector(compare:)]. Note that the result comes back as a new array rather than changing nums.

A common early stumble is the distinctive @selector() notation. Naming a method as though it were a string reflects Objective-C's dynamic nature.

In professional work, sorting is an everyday operation in an app — ranking displays, ordering search results.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)