1
class ComparedObject {
    let id: String
    let order: Int
    ...
}

class Object1 {
    let id: String
    let name: String
    ...
}

class Object2 {
    let id: String
    let x: Float
    ...
}

class Object3 {
    let id: String
    let y: Int
    ...
}

class Object4 {
    let id: String
    let z: Double
    ...
}

var comparedList: [ComparedObject] = [ComparedObject, ComparedObject, ComparedObject, ComparedObject]

var list = [AnyObject] = [Object2, Object1, Object3, Object4]

I have four classes, all with id: String attribute.

And I want to store them into one (AnyObject) array to show in UICollectionView.

So how can I sort list array with comparedList id attributes?

comparedList is already sorted.

I want sort list array from comparedList id attribute:

var comparedList = [
    ComparedObject(id: "01d7da417657", order: 0),
    ComparedObject(id: "61a96d769843", order: 1),
    ComparedObject(id: "5047abd36432", order: 2),
    ComparedObject(id: "3213f37a4003", order: 3)
]

var list = [
    Object2(id: "3213f37a4003", x: 34.0),
    Object1(id: "61a96d769843", name: "name"),
    Object3(id: "01d7da417657", y: 9),
    Object4(id: "5047abd36432", z: 0.3)
]

To this:

var list = [
    Object3(id: "01d7da417657", y: 9),
    Object1(id: "61a96d769843", name: "name"),
    Object4(id: "5047abd36432", z: 0.3),
    Object2(id: "3213f37a4003", x: 34.0)
]

Finally I did it like this:

comparedList = comparedList.sorted { $0.order < $1.order }
var comparedOrder = comparedList.map { $0.id }

list.sort { (comparedOrder.firstIndex(of: $0.id)! < comparedOrder.firstIndex(of: $1.id)! }
3

1 Answer 1

0

You just need to create a protocol that defines a requirement for an id: String property, then make all your types conform to said protocol and instead of storing them as AnyObject, store them as the protocol type. This will enable you to sort the array based on id.

protocol MyIdentifiable {
    var id: String { get }
}

struct ComparedObject: MyIdentifiable {
    let id: String
}

struct Object1: MyIdentifiable {
    let id: String
    let name: String
}

struct Object2: MyIdentifiable {
    let id: String
    let x: Float
}

struct Object3: MyIdentifiable {
    let id: String
    let y: Int
}

struct Object4: MyIdentifiable {
    let id: String
    let z: Double
}

let comparedList = [ComparedObject(id: "!")]
let list: [MyIdentifiable] = [Object1(id: "1", name: "1"), Object2(id: "2", x: 2.0)]
let sortedList = list.sorted(by: { $0.id < $1.id })
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for misunderstanding. I want to sort list array from comparedList array id attribute.
@hichkas that's a completely different question from the original. If your actual issue isn't how to store different types with some common properties in a single array and sort them based on said common properties, but the problem is how to sort an array based on the order of another array, then the question is a duplicate of Sorting a Swift Array by ordering from another Array
Thank you for your suggestion. I figured it out how to sort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.