5

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
}    

}

1
  • 1
    conform to protocol Comparable Commented Nov 4, 2018 at 15:11

3 Answers 3

4

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()
Sign up to request clarification or add additional context in comments.

Comments

3

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)

4 Comments

isn't he trying to only get the highest value in that class instance ?
altho, the answer is good, and helpful would you mind to keep it that way >:D i'll upvote it good info provided
i am not the OP we just share the same name.
I noticed - very confusing!! 😄
2

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)

2 Comments

My english isn't really good, sorry. I don't want to find out the higher value in one object. I want to find out the highest value of value1 of all objects in that array.
@Tobi check my Updated Answer

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.