How to implement natural sorting on the alphanumeric array in swift?
Sorting an array with strings and numbers in Swift

Team Lead - iOS who loves building mobile applications. Dynamic and passionate individual with extensive experience in application development lifecycle from concept through delivery of next-generation applications and customisable solutions in an Agile Environment.
With nearly eight years of development experience, three years of project management, I have a track record of working with top clients and building their business ideas. Continuous learner always looking for an enriching and strengthening my technical and professional skill set.
Expertise:
- iOS Application Development [Swift | Objective-C]
- Project Management
- Payment Gateway Integration [PayU, Razorpay, Ingenico, PayFort]
- Agile Development
- Postman for API Testing
- Unit Testing
- Design Patterns [MVC | MVVM | VIPER ]
- Version Control [GIT | SVN]
- Functional Programming
Find me Online:
- Medium: https://milanpanchal24.medium.com/
- Twitter: https://twitter.com/milan_panchal24
- HackerRank: https://www.hackerrank.com/milanpanchal?hr_r=1
- StackOverflow: https://stackoverflow.com/users/1748956/milanpanchal
- Github: https://github.com/milanpanchal
In Swift (or most of the programming language), the default method for sorting an array uses lexicographic order. That means string Foo12 does come before the string Foo2.
Example 1 (Lexical Sort):

In most cases, we perform lexical sorting on our array. But there might be a scenario where we need to use natural sorting for a better look on for end-user. (eg. Sorting File Names). In the natural soring order, the string Foo12 comes after the string Foo2.
Example 2 (Natural Sort):

The macOS Finder also uses
localizedStandardCompare:, a method to sorts filenames.

Summary:
localizedStandardCompare(_:) method should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate.
You can find me on:
Twitter | Tooter | LinkedIn | GitHub | Medium | HackerRank | LeetCode | Stack Overflow



