Jump to content
Sign in to follow this  
Conde2

table.maxValue(array)

Recommended Posts

Conde2    0
Conde2

Bom como eu participo do grupo de suporte e um cara pediu isso vim trazer aqui pra vocês.

 

Bom uso =)

 

function table.maxValue(array)
t = {}
      for _, check in pairs(array) do
          if type(check) == "number" then
            table.insert(t, check)
            end
      end
             table.sort(t, function(a,  return a > b end)
  return t[1]
end

 

Ao printar

 

print(table.maxValue({7,2, "a"}))
o valor retornado será o maior da tabela sendo assim:

 

7

Share this post


Link to post
Share on other sites
Jovial    2
Jovial

Ineficiência extrema :o

 

Desse jeito ela tem o mesmo comportamento da sua mas sem fazer a cópia do array e a ordenação, ambos desnecessários nesse caso.

function table.maxValue(array)
   local max
   for _, v in pairs(array) do
       if type(v) == "number" then
           if( not max or v > max )then
               max = v
           end
       end
   end
  return max
end

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×