Tobi
Tobi

Reputation: 67

Swift - Find highest value in custom class array with multiple variables

I'd like to find the highest value in my array. I found the method .max() in Apples documentation.

let heights = [67.5, 65.7, 64.3, 61.1, 58.5, 60.3, 64.9]
let greatestHeight = heights.max()
print(greatestHeight)
// Prints "Optional(67.5)"

My array has custom datatype. The datatype contains two variables from type integer. How can I find the highest value from one of the variables?

This is my class.

class Example: Codable {

// Variables
var value1: Int
var value2: Int

// Init
init(_value1: Int, _value2: Int) {
    value1 = _value1
    value2 = _value2
}    

}

Upvotes: 5

Views: 2695

Answers (3)

Martin R
Martin R

Reputation: 539815

As you clarified in a comment:

I want to find out the highest value of value1 of all objects in that array

That can be achieved by mapping each object to its value1 and then determining the maximum:

let maxValue1 = examples.map { $0.value1 }.max()

If the given array is really huge then it can be advantageous to use the “lazy variant” to avoid the creation of an intermediate array:

let maxValue1 = examples.lazy.map { $0.value1 }.max()

Upvotes: 4

Mohmmad S
Mohmmad S

Reputation: 5088

Well, a Class instance is not an Array so you can't access the Array functions however you can use a custom func inside to get the bigger value.

something like this is an option.

class Example: Codable {

// Variables
var value1: Int
var value2: Int

// Init
init(_value1: Int, _value2: Int) {
    value1 = _value1
    value2 = _value2
}
        func max() -> Int {
        if value1 > value2 {
            return value1
        }
        return value2
    }
}

 // USAGE
var example = Example(_value1: 10, _value2: 20)

example.max()

UPDATE: as the OP pointed out he need to compare the first value only. edit on @Ashley answer, this would solve it

as .max() will return the object the contains the highest Value1.

struct Example: Codable, Comparable {
    static func < (lhs: Example, rhs: Example) -> Bool {
        return lhs.value1 < rhs.value1
    }

    // Variables
    var value1: Int
    var value2: Int

    // Init
    init(_ value1: Int, _ value2: Int) {
        self.value1 = value1
        self.value2 = value2
    }
}


let array2 = [Example(1, 4), Example(2,3)]

print(array2.max()?.value1)

Upvotes: 2

Ashley Mills
Ashley Mills

Reputation: 53121

You can do this by conforming to the Comparable protocol and implement func < (in this case I compare the sum of the 2 values)

struct Example: Codable, Comparable {
    static func < (lhs: Example, rhs: Example) -> Bool {
        return lhs.sum < rhs.sum
    }

    // Variables
    var value1: Int
    var value2: Int

    // Init
    init(_value1: Int, _value2: Int) {
        value1 = _value1
        value2 = _value2
    }

    var sum: Int {
        return value1 + value2
    }
}


let array = [Example(_value1: 1, _value2: 4), Example(_value1: 2, _value2: 3)]

print(array.max())

//Example(_value1: 1, _value2: 4)

Upvotes: 3

Related Questions