Jump to content

table sort


klaw

Recommended Posts

  • Moderators

You can't order this table since it is already ordered.

local myTable = {
      [204] = { 50, 10 },
      [820] = { 22, 32 },
      [103] = { 42, 66 },
  }


Will look like this:

local myTable = {

      [103] = { 42, 66 },
      [204] = { 50, 10 },
      [820] = { 22, 32 },    
  }

 

If you want to order this table, you need a different format were there are no custom indexes.

Edited by IIYAMA
Link to comment
2 hours ago, IIYAMA said:

lol, you are changing the table format of the copy.

Then you can better use two linked tables, saves you a lot of performance.

This is a perfectly normal way of doing it. Sometimes you want to take some data from an existing table into a new one and use that for sorting without messing with the original. But go ahead and post your solution here, OP needs it.

Edited by Tails
Link to comment
  • Moderators
local myTable = {
	{103, 42, 66 },
	{204, 50, 10 },
	{820, 22, 32 }
}

local myTableNewFormat = {
--[[
	[103] = {103, 42, 66 }, -- shared/linked with myTable 
	[204] = {204, 50, 10 },
	[820] = {820, 22, 32 }
]]
}

for i=1,#myTable do
	myTableNewFormat[myTable[i][1]] = myTable[i]
end

As you know tables are objects and they can exist at multiple places at the same time. Which means that myTable and myTableNewFormat contains exactly the same data, but with different indexes.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...