如何比较两个对象数组?

时间:2023-01-16 11:32:19

I have a class A:

我有a班:

class A {
   var identifier: String?
   var quantity: Int = 0
}

Two arrays of A instances:

一个实例的两个数组:

var array1: [A] = [a1, a2, a3, a4]
var array2: [A] = [a5, a6, a7, a8]

I don't know which is the best way to check: array1==array2 if a1.identifier == a5.identifier, a2.identifier == a6.identifier, a3.identifier==a7.identifier, a4.identifier==a8.identifier in Swift.

我不知道哪一个是最好的检查方法:array1==array2 if a1。标识符= = a5。标识符,a2。标识符= = a6。标识符,a3.identifier = = a7。标识符,a4.identifier = = a8。在斯威夫特的标识符。

Please help me...

请帮我…

4 个解决方案

#1


9  

Assume your data like that:

假设你的数据是这样的:

struct Person
    {
        let name: String
        let id:  Int
    }

    var people1 = [
        Person(name: "Quang Hà", id: 42),
        Person(name: "Lý Hải", id: 23),
        Person(name: "Maria", id: 99)
    ]

    var people2 = [
        Person(name: "Maria yyy", id: 99),
        Person(name: "Billy", id: 42),
        Person(name: "David", id: 23)
    ]

This is the method to compare two arrays of people with id:

这是用id比较两组人的方法:

func areArrayPeopleEqual(people1:[Person], people2: [Person]) -> Bool {
    var array1 = people1
    var array2 = people2

    // Don't equal size => false
    if array1.count != array2.count {
        return false
    }

    // sort two arrays
    array1.sortInPlace() { $0.id > $1.id }
    array2.sortInPlace() {$0.id > $1.id }

    // get count of the matched items
    let result = zip(array1, array2).enumerate().filter() {
        $1.0.id == $1.1.id
        }.count

    if result == array1.count {
        return true
    }

    return false
}

#2


15  

You can try like this:

你可以这样尝试:

let result = zip(array1, array2).enumerate().filter() {
    $1.0 == $1.1
}.map{$0.0}

#3


2  

First we extend Equatable class, to have a DRY code, than if the 2 arrays are always of the same size, or if at least the first one is <= than the second you can go with this solution.

首先,我们扩展了Equatable类,使其具有一个DRY代码,而如果两个数组总是相同大小,或者至少第一个数组是<=,那么您可以使用这个解决方案。

Pay attention that you are working with optionals, you may have to unwrap them before.

注意,你正在处理选项,你可能需要在之前打开它们。

class A {
    var identifier: String?
    var quantity: Int = 0

    init(identifier: String, quantity: Int) {
        self.identifier = identifier
        self.quantity = quantity
    }
}

let a1: A = A(identifier: "1", quantity: 1)
let a2: A = A(identifier: "2", quantity: 2)
let a3: A = A(identifier: "3", quantity: 3)
let a4: A = A(identifier: "4", quantity: 4)

let a5: A = A(identifier: "1", quantity: 1)
let a6: A = A(identifier: "2", quantity: 2)
let a7: A = A(identifier: "3", quantity: 3)
let a8: A = A(identifier: "4", quantity: 4)

var array1: [A] = [a1, a2, a3, a4]
var array2: [A] = [a5, a6, a7, a8]

func areEquals(array1: [A], array2: [A]) -> Bool {
    if array1.count < array2.count {
        return false
    }
    for i in 0...array2.count - 1 {
        if array1[i] != array2[i] {
            return false
        }
    }
    return true
}


extension A: Equatable {
    static func ==(lhs: A, rhs: A) -> Bool {
        //you can choose how and when they should be equals
        return lhs.identifier == rhs.identifier
    }
}

如何比较两个对象数组?

#4


1  

try this code, let me know if it works

试试这段代码,如果有用就告诉我

func toDictionary<E, K, V>(
    array:       [E],
    transformer: (element: E) -> (key: K, value: V)?)
    -> Dictionary<K, V>
{
    return array.reduce([:]) {
        (var dict, e) in
        if let (key, value) = transformer(element: e)
        {
            dict[key] = value
        }
        return dict
    }
}

then you can execute a check like below

然后可以执行如下所示的检查

let areEqual = array1.count == array2.count;
if areEqual {
    let dict1 = toDictionary(array1) { ($0.identifier, $0.quantity) }
    let dict2 = toDictionary(array2) { ($0.identifier, $0.quantity) }
    areEqual = NSDictionary(dictionary: dict1).isEqualToDictionary(dict2)
}
print(areEqual)

disclaimer: function toDictionary has been took form here

免责声明:函数toDictionary在这里已经形成

#1


9  

Assume your data like that:

假设你的数据是这样的:

struct Person
    {
        let name: String
        let id:  Int
    }

    var people1 = [
        Person(name: "Quang Hà", id: 42),
        Person(name: "Lý Hải", id: 23),
        Person(name: "Maria", id: 99)
    ]

    var people2 = [
        Person(name: "Maria yyy", id: 99),
        Person(name: "Billy", id: 42),
        Person(name: "David", id: 23)
    ]

This is the method to compare two arrays of people with id:

这是用id比较两组人的方法:

func areArrayPeopleEqual(people1:[Person], people2: [Person]) -> Bool {
    var array1 = people1
    var array2 = people2

    // Don't equal size => false
    if array1.count != array2.count {
        return false
    }

    // sort two arrays
    array1.sortInPlace() { $0.id > $1.id }
    array2.sortInPlace() {$0.id > $1.id }

    // get count of the matched items
    let result = zip(array1, array2).enumerate().filter() {
        $1.0.id == $1.1.id
        }.count

    if result == array1.count {
        return true
    }

    return false
}

#2


15  

You can try like this:

你可以这样尝试:

let result = zip(array1, array2).enumerate().filter() {
    $1.0 == $1.1
}.map{$0.0}

#3


2  

First we extend Equatable class, to have a DRY code, than if the 2 arrays are always of the same size, or if at least the first one is <= than the second you can go with this solution.

首先,我们扩展了Equatable类,使其具有一个DRY代码,而如果两个数组总是相同大小,或者至少第一个数组是<=,那么您可以使用这个解决方案。

Pay attention that you are working with optionals, you may have to unwrap them before.

注意,你正在处理选项,你可能需要在之前打开它们。

class A {
    var identifier: String?
    var quantity: Int = 0

    init(identifier: String, quantity: Int) {
        self.identifier = identifier
        self.quantity = quantity
    }
}

let a1: A = A(identifier: "1", quantity: 1)
let a2: A = A(identifier: "2", quantity: 2)
let a3: A = A(identifier: "3", quantity: 3)
let a4: A = A(identifier: "4", quantity: 4)

let a5: A = A(identifier: "1", quantity: 1)
let a6: A = A(identifier: "2", quantity: 2)
let a7: A = A(identifier: "3", quantity: 3)
let a8: A = A(identifier: "4", quantity: 4)

var array1: [A] = [a1, a2, a3, a4]
var array2: [A] = [a5, a6, a7, a8]

func areEquals(array1: [A], array2: [A]) -> Bool {
    if array1.count < array2.count {
        return false
    }
    for i in 0...array2.count - 1 {
        if array1[i] != array2[i] {
            return false
        }
    }
    return true
}


extension A: Equatable {
    static func ==(lhs: A, rhs: A) -> Bool {
        //you can choose how and when they should be equals
        return lhs.identifier == rhs.identifier
    }
}

如何比较两个对象数组?

#4


1  

try this code, let me know if it works

试试这段代码,如果有用就告诉我

func toDictionary<E, K, V>(
    array:       [E],
    transformer: (element: E) -> (key: K, value: V)?)
    -> Dictionary<K, V>
{
    return array.reduce([:]) {
        (var dict, e) in
        if let (key, value) = transformer(element: e)
        {
            dict[key] = value
        }
        return dict
    }
}

then you can execute a check like below

然后可以执行如下所示的检查

let areEqual = array1.count == array2.count;
if areEqual {
    let dict1 = toDictionary(array1) { ($0.identifier, $0.quantity) }
    let dict2 = toDictionary(array2) { ($0.identifier, $0.quantity) }
    areEqual = NSDictionary(dictionary: dict1).isEqualToDictionary(dict2)
}
print(areEqual)

disclaimer: function toDictionary has been took form here

免责声明:函数toDictionary在这里已经形成