0Pricing
Swift Academy · Lesson

Sorting, Searching and Slicing

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

Sorting, Searching and Slicing is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “Sorting, Searching and Slicing” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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