Sorting tables by a value

  1. What do you want to achieve?
    I’m trying to add a player object and a value to a table then sorting it by the value (high to low).

  2. What is the issue?
    Pretty much just can’t get it to work for some reason.

  3. What solutions have you tried so far?
    I’ve tried quite a few things and haven’t been able to get it to work.

This is how I’m adding the player to the table:

table.insert(UnsortedTable, {Player, Player.Points.Value})

Then I’m trying to sort it with this:

local SortedTable = table.sort(UnsortedTable,
	function(Value1, Value2)
		return Value1[2] > Value2[2]
	end
)

I don’t know what I’m doing wrong, might be a misunderstanding of table.sort, any help is much appreciated.

2 Likes

Try this?

local function Sort(Value1, Value2)
     return Value1[2] > Value2[2]
end

local SortedTable = table.sort(UnsortedTable, Sort)
print(SortedTable)
2 Likes

Still returning nil, I think this is just a different way of doing the same thing.

EDIT: May have just thought of something that might be breaking it, if there is only one value in the table then I assume it would not add anything to sorted table, correct?

2 Likes

That could be the case, try adding 3 or 4 values then attempt to sort them again

2 Likes

Are you trying to use SortedTable anytime later in your script?

If yes, then that’s the issue. table.sort doesn’t return the sorted table, instead, it just sorts the table that got passed to it.

2 Likes

I am using it later, what does the SortedTable equal then? How am I supposed to achieve what I want?

2 Likes

After you sorted your table using table.sort, you can just use the table again like normal.

All that table.sort does is sort your table, it doesn’t really return anything after that.

3 Likes

Oh… So sorted table is literally just sorting the table you feed it? Even if I set it equal to SortedTable then it won’t set make that the new table?

I feel like if I set a value equal to a table.sort then it would set the variable (SortedTable) equal to the table.

EDIT: So I could just do this?:

local function Sort(Value1, Value2)
	return Value1[2] > Value2[2]
end

table.sort(Table, Sort)
print(Table)
2 Likes

Unfortunately, table.sort doesn’t do that.

However, if you’d like to achieve that result, you could do something like this instead:

local SortedTable = UnsortedTable
	
table.sort(SortedTable,
	function(Value1, Value2)
		return Value1[2] > Value2[2]
	end
)

By the way, when I first posted this I made a mistake (which I already fixed now). Remember to set the table that is used in table.sort’s first argument to SortedTable, not UnsortedTable (just like in the script above).

4 Likes

That should work, but you have a minor mistake. It should be this:

local Table = {} --contains values to be sorted...

local function Sort(Value1, Value2)
	return Value1 > Value2
end

table.sort(Table, Sort)
print(Table) --> prints sorted
2 Likes

Tables are not implicitly copied with the assignment operator, so doing local SortedTable = UnsortedTable will only create a variable SortedTable that is a direct reference to UnsortedTable. You can just pass the single variable to table.sort

For copying the unsorted table, you would have to do a shallow copy like this:

local SortedTable = table.pack(table.unpack(UnsortedTable))
--and then
table.sort(SortedTable, Sort)
1 Like

Because it’s a table it would still be Value1[2] and Value2[2].

Right. I didn’t realize that you were storing tables in the table. That also means the shallow copy example in my previous reply wouldn’t work as you would need a deep copy, but it doesn’t matter anyways because you don’t need that.

1 Like