How would I find the 4 lowest values and then be able to link the values back to an original "object" (clarified on a post)

New info, read my latest post on this thread
So, I have a system where an array with a bunch of strings gets sent with a event to another script, and then I use clever string methods to extract the individual values that I want from the big string. (so essentially I have a bunch of different values sent as one big string and then there are multiple of those strings in an array then it is deconstructed and the individual values are there) Then, what I would need to do is take a value from it and compare it with a constant value, and see how big the resulting integer is (distance). However, I don’t know how to figure out how to find the 4 lowest values once I have gotten all of the distances. Does anyone know how to do this?

TL;DR: I have an array of Integers. How do I find the 4 lowest?

I’ve never used this before but such thing as table.sort() does exist. It’s listed in this API reference about table functions and it’s probably going to be the easiest method to sort a table and then use the first 4 key & value pairs found in the sorted array.

You can sort it.

local tableE = {4, 66, 1, 83, 235, 652, 7, 44}

table.sort(tableE, function(a, b)
 return a > b -- sorts the table from highest to lowest
end)

So would I want to use

return a < b

if I want to make to lowest to highest?

Yes, that is correct to get the lowest to highest.

Ok. New problem. I would have sorted the values into the lowest to highest, however, once I have the 4 lowest values, I need to figure out how to get the original object that they corresponded to.
Why do I need this? Well, I am making a train departure system, and what I am using this for is to find the 4 closest trains and display that info on a screen. However, I need info like the destination so I can display that correctly. Should I be figuring out the 4 lowest a different way, or is there a way to figure out which value goes to which train?

Note: I didn’t make a new thread as I didn’t want to add extra threads into this section.

Could you pass the whole object into the table, and do something like this?

{id: 123, destination: "234"}

Then, when table sorting do

table.sort(table, function(a, b)
 return a.id < b.id
end)