-1

I want to order an object by a parameter. But this parameter is not a value but an array of values.

class MyObject:{
    var arrayOfDoubles: [Double]
}

I´ve solved how to order the param arrayOfDoubles

self.arrayOfDoubles.sorted(by: >)

My problem now is how to order the array myObjects by the param arrayOfDoubles

myObjects: [MyObject]

I´ve tried this solution, but it only works when the param is a number, not an array of numbers myObjects.sorted(by: { $0.arrayOfDoubles > $1.arrayOfDoubles })

jotape
  • 319
  • 2
  • 6
  • 14
  • Possible duplicate of [Swift how to sort array of custom objects by property value](https://stackoverflow.com/questions/24130026/swift-how-to-sort-array-of-custom-objects-by-property-value) – Dávid Pásztor Aug 15 '18 at 18:45
  • What is the condition for sorting? – Rakesha Shastri Aug 15 '18 at 18:46
  • @RakeshaShastri decresing – jotape Aug 15 '18 at 18:47
  • What do you mean by that? It has an array, how can an array be decreasing? Elements in the array can be decreasing. – Rakesha Shastri Aug 15 '18 at 18:48
  • @DávidPásztor I think is not. Because that post is how to order an object by a param that is a number. My param is array of number. I´ve tried that solution but does not compile – jotape Aug 15 '18 at 18:48
  • Array 1 [100, 40] is bigger than Array 2 [20, 50]. Is this clear? – jotape Aug 15 '18 at 18:50
  • 1
    You mean the sum of the elements in the array? – Rakesha Shastri Aug 15 '18 at 18:51
  • How are you defining what the sort is? The above question answers what you're trying to do, the only thing that isn't answered in there is how you define which array should appear before the other because there's not really a defined sort for a comparison like this. – jlowe Aug 15 '18 at 18:52
  • @jlowe the condition should be "Comparing each index of the arrays, until an index value is bigger than the other. That is the condition. e.g: [20, 40] is bigger than [10,0] and [20, 40] is bigger than [20, 30]. All the individual arrays are always sorted decreasingly also. – jotape Aug 15 '18 at 18:57
  • Your "doubles" should be a custom struct. Also you need to explain what "comparing each index" means; I still don't know how to tell whether one double is bigger than another double, and if I don't know, I can't tell a computer how to know. – matt Aug 15 '18 at 19:17
  • Also you say "decreasing" but your code says `>` which means increasing. So it is unclear even what sort direction you want. – matt Aug 15 '18 at 19:18
  • > is decreasing. But I agree that he show some of his examples sorted increasingly – Leo Dabus Aug 15 '18 at 19:23

1 Answers1

1

This might not be exactly what you want, because I can't figure out what you want (and I'm not sure you do either). But in general your problems will be over if you define a custom struct and make it Equatable and Comparable. Then you can just sort using > or < directly, like this:

struct Pair : Comparable {
    let ix1:Int
    let ix2:Int
    init(_ ix1: Int, _ ix2:Int) {
        self.ix1 = ix1; self.ix2 = ix2
    }
    static func ==(lhs:Pair, rhs:Pair) -> Bool {
        return lhs.ix1 == rhs.ix1 && lhs.ix2 == rhs.ix2
    }
    static func <(lhs:Pair, rhs:Pair) -> Bool {
        return lhs.ix1 < rhs.ix1 && lhs.ix2 < rhs.ix2
    }
}

let array1 = [Pair(1,3), Pair(10,11), Pair(0,1)]
let array2 = array1.sorted(by:>)
// [{ix1 10, ix2 11}, {ix1 1, ix2 3}, {ix1 0, ix2 1}]

As I say, that's only an example; tweak it so that it says what you mean (if you even know what you mean).

For instance, if this Pair is now to be a property of another object type Obj, that's trivial in just the same way:

struct Obj {
    let pair : Pair
}
let array3 = [Obj(pair:Pair(1,3)), Obj(pair:Pair(10,11)), Obj(pair:Pair(0,1))]
let array4 = array3.sorted {$0.pair > $1.pair}

I believe that's the sort of language your question says you'd like to use...

matt
  • 515,959
  • 87
  • 875
  • 1,141