0

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]
  • Could you explain why toy want such a strange sort order and the full logic behind it? – Joakim Danielson May 21 '21 at 10:10
  • `Expected sort` Please explain the logic behind such an expectation. Why this order? What are the constraints? – Eric Aya May 21 '21 at 10:12
  • I updated the question. Mainly looking for a sort after the decimal as an integer, but within the first number before the decimal point. – Christian Ray Leovido May 21 '21 at 10:22
  • 1
    Ok so I think you're looking for [this](https://stackoverflow.com/q/35815469/2227743) (numerical sort) – Eric Aya May 21 '21 at 10:25
  • I think you should to use some string format to convert 2.2 to 2.02 and then sort. And while displaying use string format – Raja Kishan May 21 '21 at 10:29
  • 1
    This quote from Linus Torvalds comes to mind: _Bad programmers worry about the code. Good programmers worry about data structures and their relationships._ There is no logical reason whatsoever for storing these `order` values as double-precision floats. Why not use two `short int` values instead? (For example, `section` and `subsection`) – r3mainer May 21 '21 at 10:40
  • Why is Model using a Double? It seems like a "chapter separations list". It be easier it they were `String`, or array of `Int`. – Larme May 21 '21 at 10:41
  • @r3mainer I did think about that just after I posted the question and I'm working on that solution atm – Christian Ray Leovido May 21 '21 at 10:42
  • @Larme the original model is using a `String` and I am trying different ways of sorting with different types. I'm trying an approach with `Int`s with section and subsection as @r3mainer suggests above. – Christian Ray Leovido May 21 '21 at 10:44
  • 1
    With String, it's exactly the question in https://stackoverflow.com/questions/35815469/swift-sort-array-of-versions-as-strings as linked by @EricAya – Larme May 21 '21 at 10:45
  • 2
    You're welcome. I'm marking the page as duplicate so that the solution is more visible. – Eric Aya May 21 '21 at 10:57

0 Answers0