Swift Academy · Lesson

Sorting, Searching and Slicing

sorted(), sort(), firstIndex(of:), prefix, suffix and ArraySlice.

Lesson 2 of 412 steps

Sorting, Searching and Slicing is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

Swift arrays come with a rich set of algorithms for sorting, searching, and slicing. Learning them reduces hand-written loops and produces cleaner, faster code.

sorted() vs sort()

```swift let nums = [3,1,4,1,5,9] let sorted = nums.sorted() // new array, nums unchanged var mutable = [3,1,4] mutable.sort() // in-place sort ``` `sorted()` works on `let` arrays; `sort()` requires `var`.

Custom Sort Order

```swift let words = ["banana","apple","cherry"] let alpha = words.sorted() // ascending let rev = words.sorted(by: >) // descending let byLen = words.sorted { $0.count < $1.count } // by length ```

first(where:) and last(where:)

```swift let nums = [4, 7, 2, 9, 1] let first = nums.first { $0 > 5 } // Optional(7) let last = nums.last { $0 > 5 } // Optional(9) ``` Better than a manual loop — stops at the first match.

Binary Search with partition(by:)

Swift's stdlib uses partition for efficient O(n) partitioning: ```swift var nums = [3, 1, 4, 1, 5, 9, 2] let pivotIdx = nums.partition { $0 >= 5 } // nums is now [...<5 values..., 5,9] and pivotIdx is the split point ```

Array Slices: prefix and suffix

```swift let arr = [10,20,30,40,50] let first3 = arr.prefix(3) // ArraySlice [10,20,30] let last2 = arr.suffix(2) // ArraySlice [40,50] let mid = arr[1...3] // ArraySlice [20,30,40] ``` Slices are views — they share storage with the original array.

Converting Slice to Array

```swift let slice = arr.prefix(3) let copy = Array(slice) // independent [Int] copy ``` Always convert a slice to `Array` if you need to store it beyond the source array's lifetime.

drop and dropFirst/dropLast

```swift let arr = [1,2,3,4,5] let d1 = arr.dropFirst() // [2,3,4,5] let d2 = arr.dropLast(2) // [1,2,3] let d3 = arr.drop { $0 < 3 } // [3,4,5] — drop while condition holds ```

min() and max()

```swift let scores = [88, 95, 70, 82] print(scores.min()!) // 70 print(scores.max()!) // 95 let minByLen = ["cat","elephant","ox"].min { $0.count < $1.count } // "ox" ```

contains and allSatisfy

```swift let nums = [2, 4, 6, 8] print(nums.allSatisfy { $0 % 2 == 0 }) // true print(nums.contains { $0 > 10 }) // false ``` `allSatisfy` and `contains(where:)` short-circuit early.

Quick Check

Which method returns the first N elements of an array as an `ArraySlice`?

Recap

Key takeaways: • `sorted()` → new array; `sort()` → mutates in place • `first(where:)` / `last(where:)` — find elements • `prefix`, `suffix`, `[range]` — slicing (no copy) • `drop(while:)`, `dropFirst`, `dropLast` — remove from ends • `min()`, `max()`, `allSatisfy`, `contains(where:)` Next: map, filter and reduce.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Sorting, Searching and Slicing” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Sorting, Searching and Slicing”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Sorting, Searching and Slicing”?

sorted(), sort(), firstIndex(of:), prefix, suffix and ArraySlice. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Sorting, Searching and Slicing” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating and Mutating Arrays
  2. Sorting, Searching and Slicing
  3. Map, Filter and Reduce on Arrays
  4. 2D Arrays and Nested Collections
← Back to Swift Academy