I have the following struct and want to sort the models by order.
struct Model {
let name: String
let order: Double
}
let models = [
Model(name: "Fancy name", order: 2.1),
Model(name: "Not so fancy name", order: 2.2),
Model(name: "Unfanciest name", order: 2.15),
Model(name: "The fanciest", order: 2.16)
]
// Current result: [2.1, 2.15, 2.16, 2.2]
// models.sorted(by: { $0.order < $1.order })
// Expected result: [2.1, 2.2, 2.15, 2.16]
// ???
Currently this is how it sorts using the <
operator.
Current sort: [2.1, 2.15, 2.16, 2.2]
I'd like to sort it the following way, taking into account the first number and the numbers after the dot.
This is usually the way it would be sorted when you are writing a document and have bullet points. You have a first section and a subsection. e.g. Section 1, subsection 1, 2, 3, n. The whole section should be sorted in ascending order, 1.1, 1.2, 1.3 ... 1.15, 1.20, 1.21 etc. The main difference with the normal ascending sort, is that it takes it should be sorted by the number after the decimal point within the number before the decimal point. In the example above, 15 is greater than 2 and should appear after 2.
Expected sort: [2.1, 2.2, 2.15, 2.16]