Swift Sort array of objects alphabetically
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Sorting an array of custom objects alphabetically in Swift usually means comparing one string property, such as name or title. The mechanics are simple, but the details matter: you need to decide whether to mutate the original array, whether the comparison should be case-sensitive, and how to handle optional or localized text. Good sorting code is less about clever syntax and more about making those rules explicit.
Choose Between sorted() and sort()
Swift gives you two closely related APIs. sorted() returns a new array and leaves the original unchanged. sort() mutates the existing array in place.
If you no longer need the original order, mutating in place is often cleaner.
The rule is simple: use sorted() when you want a new value, and sort() when you want to update the existing array.
Compare the Right Property
Custom objects are not “special” to the sort algorithm. Swift just needs a closure that says which element should come first. For alphabetical sorting, that closure usually compares a string property directly.
This works for structs and classes alike because the comparison is based on the property value, not on the object type itself.
Use Localized Comparisons for User-Facing Lists
Raw string comparison with < is fine for internal data, but it is not always the best match for what users expect in a visible list. User-facing alphabetical order usually benefits from localized and case-insensitive comparison.
This produces a more natural order for UI lists because it does not treat uppercase and lowercase as separate alphabets.
If the app is multilingual, localized comparison is usually the better default than a strict binary comparison.
Handle Optional Strings Deliberately
If the property you want to sort by is optional, decide what nil means before you write the comparator. Treating nil as an empty string is common, but it is only correct if missing text should appear first.
Another reasonable rule is to put nil values last. The important part is to encode the rule directly instead of letting optional handling become an afterthought.
Add Tie-Breakers When Names Are Not Unique
Alphabetical sorting often becomes ambiguous when two objects share the same primary string field. A secondary comparison keeps the result deterministic.
That extra rule matters in tests, exported files, and UI lists where unstable ordering creates unnecessary noise.
When Comparable Is Worth It
If the same object type is sorted the same way throughout the app, implementing Comparable can make the call sites cleaner.
For a one-off sort, a closure is enough. For a repeated domain rule, Comparable can be a worthwhile improvement.
Common Pitfalls
- Using
sort()when you meant to preserve the original array order. - Comparing raw strings with
<for UI data that should use localized or case-insensitive ordering. - Ignoring
nilhandling when the sort key is optional. - Forgetting secondary sort rules when duplicate names are common.
- Creating temporary display strings for sorting when the real model property should be compared directly.
Summary
- '
sorted()returns a new alphabetically ordered array, whilesort()mutates in place.' - Sorting custom objects is just a matter of comparing the correct string property.
- User-facing lists usually benefit from localized, case-insensitive comparison.
- Optional sort keys need an explicit rule for how missing values should be placed.
- Add tie-breakers or
Comparablewhen the sort order is part of the domain model.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.