1

I have a program that evaluates sets of images and assigns them a given value, now I would like to sort the output of this program, to do this I have the following code:

 function SelectTop(params,images,count)
    local values={}  
    for k,v in pairs(images) do
        local noError,res=pcall(evaluate,params,v)

    if noError then 
        values[v]=res
    else

    values[v] = 9999999999999999999999999999999999999999999999999999999999
    end
  end
function compare(a,b)
  return a[2] < b[2]
end

  table.sort(values,compare)
  print(values)
end

where we can reasonably assume the output of evaluate to be akin to math.random(7000) (the actual code is far more complex and involves neural networks). Now I would expect the output to be sorted but instead I get something like this:

 {
  table: 0x40299d30 : 4512.3590053809
  table: 0x40299580 : 4029.3450116073
  table: 0x40298dd0 : 6003.9508240314
  table: 0x40297de0 : 6959.9145312802
  table: 0x40297630 : 4265.2784117677
  table: 0x40296e40 : 3850.0829011681
  table: 0x40296690 : 4007.2308907069
  table: 0x40296ec0 : 3840.5216952082
  table: 0x4029a770 : 5059.1475464564
  table: 0x40299fc0 : 6058.9603651599
  table: 0x40299810 : 1e+58
  table: 0x40299060 : 1e+58
  table: 0x402988b0 : 5887.729117754
  table: 0x402978c0 : 3675.7295252455
  table: 0x40296920 : 1e+58
  table: 0x4029aa00 : 5624.6042279879
  table: 0x40295bf8 : 1391.8185365923
  table: 0x40296458 : 4276.09869066
  table: 0x40299aa0 : 1e+58
  table: 0x402992f0 : 6334.3641972965
  table: 0x40298300 : 2660.5004512843
  table: 0x40298b40 : 6200.373787482
  table: 0x40296148 : 6178.926312832
  table: 0x40298380 : 1559.5307868896
  table: 0x40295968 : 1e+58
  table: 0x40296bb0 : 6708.7545218628
  table: 0x4029b550 : 1484.2931717456
  table: 0x40298400 : 1638.1286256175
  table: 0x40298070 : 3762.7368939272
  table: 0x402963d8 : 1500.002116023
  table: 0x4029ac90 : 2486.2695974502
  table: 0x40295e88 : 1e+58
  table: 0x40297b50 : 4806.6468870717
  table: 0x4029a4e0 : 4328.0636461426
  table: 0x402973a0 : 4757.4343171052
  table: 0x4029a250 : 3998.8649821268
}

So why does table.sort not work here? I would assume that some sort of sorting would happen here?

Anybody know what I'm doing wrong?

So if we want a full example we can do something like this:

function evaluate (a,b)
    return math.random(7000)
end
SelectTop(nil,{ {a, b, c}, {d, e, f}, {g, e, f}, {f, e, f} },0)

output:

{ table: 0x41c2af18 : 5560
  table: 0x41c2afa8 : 4131
  table: 0x41c2af60 : 4892
  table: 0x41c2aff0 : 5273
}
1
  • table.sort is used to sort an array like table, i.e, the index must be integers 1, 2, ... to n, while values isn't one such table. Sorting a hash like table makes little sense. Commented May 2, 2017 at 10:11

1 Answer 1

4

table.sort works on arrays, not on dictionaries.

You'll need to replace values[v]=res with something like values[#values+1]= {v, res} and adjust compare accordingly.

Right now table.sort will see empty array - there's no items idx 1/2/3/..., because you're indexing results with image itself.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that's indeed the issue!

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.