Eliminate array .sorted errors Swift 5

Omega_Pixel

I have an array of chapters and units.

var chaptersAndUnits: [String] = ["Chapter 2", "Chapter 3", "Chapter 5", "Unit 12", "Chapter 40"]

I am loading them into a UIPickerView so I order them with chaptersAndUnits = chaptersAndUnits.sorted().

However, after this process I get an erratic value: ["Chapter 2", "Chapter 3", "Chapter 40", "Chapter 5", "Unit 12"].

I would like to eliminate that Chapter 40 is inserted after Chapter 3. This also occurs with values like Chapter 22, it will insert it after Chapter 1.

What do I need to do to remove these errors? I have looked into regex expressions, but couldn't make them work.

How would I go about writing my own sorter to fix this?

TIA!

flanker

This is the expected behaviour. You are comparing strings, not numbers, and "Chapter 40" > "Chapter 5" == true as "4" > "5" alphabetically and they are the characters in the same positions in the strings.

If you want to retain the data as strings and order the way you want you will need a custom sorting function. Based on the example strings you could use something like


func bookLessThan(_ lhs: String, _ rhs: String) -> Bool {
   let lhsComponents = lhs.components(separatedBy: " ")
   let rhsComponents = rhs.components(separatedBy: " ")
   return lhsComponents.first! != rhsComponents.first! ? lhsComponents.first! < rhsComponents.first! :
   Int(lhsComponents.last!)! < Int(rhsComponents.last!)!
}

var chaptersAndUnits: [String] = ["Chapter 2", "Chapter 3", "Chapter 5", "Unit 12", "Chapter 40"]

let sorted = chaptersAndUnits.sorted(by: bookLessThan)

print(sorted)
// ["Chapter 2", "Chapter 3", "Chapter 5", "Chapter 40", "Unit 12"]

NB: In practice you'd want to safely unwrap all the optionals in case your strings aren't all in the expected format, and then handle that, maybe by returning a false value so they are all at the end.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extending Array to check if it is sorted in Swift?

How do I insert an element at the correct position into a sorted array in Swift?

Swift: A sorted dictionary

Swift memory leak when iterating array of Errors

Handling Firebase 5 Authentication Errors In Swift 4

Incorrect Array Sorting when using Swift sorted(by:) method

Errors when update code to avoid deprecation warnings withUnsafeMutableBytes in swift 5

Swift 5 dynamic type for array

Swift 5: JSON object array to swift structure

Update JSON Array in Swift 5

How to eliminate EDID checksum errors?

Eliminate duplicates in array (JSONiq)

Yield within Set to eliminate in an Array

Errors when try to delete allocated array l_sorted and r_sorted in c++

Unable to mount raid 5 array (mdadm), with no drive errors

Cannot assign sorted array to a variable in Swift

A type for sorted arrays in Swift

How do I put a specific integer into the middle of a sorted array in Swift?

Eliminate repeated code Swift 3

"Sorted(by: )" not returning an Array / how to sort an Array of PFObjects in swift

Swift - comparing 2 arrays by id, and merge into new sorted Array

Custom operation to split sorted array into subarrays in Swift

accessing keys from a returned sorted array of dictionaries in Swift 4

Sorted Array/Map Swift

swift Ekevents to 2 dimensional array sorted by Date

Convert Json to Array in Swift 5

Convert dictionary to array - Swift 5

Eliminate for loop when indexing into array

Find and store arrays of 5 consecutive numbers from a larger sorted array