Swift Beta Performance: Array Sorting — What Went Wrong?
\n\nIf you ever visited Stack Overflow during the era of the first Swift beta versions, you surely remember the legendary question that gathered thousands of views and hundreds of comments. It went something like this: "Why is array sorting in Swift Beta 100 times slower than in Objective-C?"
\n\nBack in 2014, this was a real shock for the iOS community. Developers, accustomed to lightning-fast sorting in Objective-C, suddenly saw their applications freeze for no apparent reason. Today, years later, we can calmly analyze this problem: understand what exactly caused the slowdowns, how Swift evolved, and, most importantly, how to sort arrays correctly in modern Swift to avoid repeating past mistakes.
\n\nIn this article, we won't just retell an old bug. We will conduct our own tests, write code, and find out which sorting algorithms in Swift are truly fast, and which are a trap for beginners. Let's go!
\n\nA Brief Background: What Happened in Swift Beta?
\n\nIn the first beta version of Swift, sorting an array of 100,000 integers could take up to 20 seconds. In Objective-C, the same operation was performed in 0.2 seconds. A 100x difference! The reason was that early Swift used an inefficient implementation of the sorting algorithm (Introsort with a poor pivot selection), and also had optimization issues when compiling without optimization flags (-O).
\n\nAdditionally, the beta version frequently triggered the dynamic bounds checking mechanism for every array access, which added enormous overhead. The conclusion was simple: Swift was raw, but its potential was enormous.
\n\nHow to Sort Arrays in Swift Today? Best Practices
\n\nModern Swift (versions 5.x and newer) is a mature language. Array sorting has become not just fast, but one of the fastest among mainstream languages. Let's break down the main methods.
\n\n1. The Built-in sort() Method — The Gold Standard
\n\nThe simplest and most recommended way is to use the sort() method (in-place sorting) or sorted() (returns a new sorted array).
import Foundation\n\nvar numbers = [5, 3, 8, 1, 9, 2]\nnumbers.sort() // in-place sorting\nprint(numbers) // [1, 2, 3, 5, 8, 9]\n\nlet sortedNumbers = numbers.sorted() // returns a new array\nprint(sortedNumbers) // [1, 2, 3, 5, 8, 9]\n\nInternally, sort() uses TimSort — a hybrid algorithm that runs in O(n log n) on average and O(n) on nearly sorted data. This is the same algorithm used in Python and Java. It is excellently optimized for modern processors.
2. Sorting with a Closure: Customization
\n\nIf you need to sort an array in descending order or by a complex criterion, use a closure:
\n\nlet words = ["apple", "orange", "banana", "kiwi"]\nlet sortedByLength = words.sorted { $0.count < $1.count }\nprint(sortedByLength) // ["kiwi", "apple", "banana", "orange"]\n\nlet descendingNumbers = numbers.sorted(by: >)\nprint(descendingNumbers) // [9, 8, 5, 3, 2, 1]\n\nImportant: the closure must be a pure function — without side effects. Otherwise, you risk unpredictable behavior and performance degradation.
\n\n3. Sorting Custom Structures
\n\nWhen working with your own data types, it's best to implement the Comparable protocol. This allows using sort() without a closure.
struct Person: Comparable {\n let name: String\n let age: Int\n\n static func < (lhs: Person, rhs: Person) -> Bool {\n return lhs.age < rhs.age // sort by age\n }\n}\n\nle