Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the max value in a Swift object array?

Tags:

arrays

swift

Let say I got an array with Usr objects. And the Usr object have the attribute age. Except from reading the Usrobject one by one, and compare the age value one by one, is there any shortcuts to do so? Thx.

like image 370
DNB5brims Avatar asked Jan 10 '15 11:01

DNB5brims


People also ask

How do you find the highest number in Swift?

Finding largest number using max() function Swift provides an in-built method named as max() function. This function returns the maximum number among the given numbers.

What is the maximum value of an array?

M = max( A ) returns the maximum elements of an array. If A is a vector, then max(A) returns the maximum of A . If A is a matrix, then max(A) is a row vector containing the maximum value of each column of A .


1 Answers

Swift 3:

let users = [Usr(15), Usr(25), Usr(20)]
let max = users.map { $0.age }.max()

// max = 25

like image 118
Marcos Reboucas Avatar answered Oct 27 '22 17:10

Marcos Reboucas