generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: How can I sort a dictionary?

Previous Thread :: Next Thread 
Trioxide is not online. Trioxide
Joined: 29 Mar 2011
Total Posts: 32902
03 May 2014 09:46 AM
-- table.sort doesn't work.

local t = {
["a"] = 6,
["b"] = 46,
["c"] = 66,
["d"] = 16,
["e"] = 456,
["f"] = 776,
}

table.sort(t)
for i,v in pairs(t) do print(i,v) end

Output:
>a 6
>c 66
>b 46
>e 456
>d 16
>f 776
Report Abuse
Trioxide is not online. Trioxide
Joined: 29 Mar 2011
Total Posts: 32902
03 May 2014 09:50 AM
*Without doing tons of loops
Report Abuse
L0cky2013 is not online. L0cky2013
Joined: 30 Jul 2012
Total Posts: 1446
03 May 2014 09:50 AM
table.sort(t, function (b, c) return b > c end)
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
03 May 2014 09:51 AM
Some Googling has told me that table.sort does not work for dictionary tables, so there's is this workaround:

Example:

myTable = {}
myTable.Z = 89
myTable.X = 8
myTable.Y = 55

sortTable = {}

for index in pairs(myTable) do
table.insert(sortTable, index)
end

sortTable[1] --> Y
sortTable[2] --> X
sortTable[3] --> Z

now the sortTable can be arranged accordingly

table.sort(sortTable)

sortTable[1] --> X
sortTable[2] --> Y
sortTable[3] --> Z


Source: http://lua.gts-stolberg.de/en/table.php#table.sort_(table_[,_comp])
Report Abuse
Trioxide is not online. Trioxide
Joined: 29 Mar 2011
Total Posts: 32902
03 May 2014 10:08 AM
local myTable = {x = 10, y = 5, z = 15} --// ORDER SHOULD BE Y, X, Z
local sortedTable = {}

for index,value in pairs(myTable) do
table.insert(sortedTable,index)
end

print(sortedTable[1]) --> Y
print(sortedTable[2]) --> X
print(sortedTable[3],"\n") --> Z

table.sort(sortedTable)

print(sortedTable[1]) --> X
print(sortedTable[2]) --> Y
print(sortedTable[3],"\n") --> Z

lel
Report Abuse
Trioxide is not online. Trioxide
Joined: 29 Mar 2011
Total Posts: 32902
03 May 2014 10:12 AM
This works:

local myTable = {x = 10, y = 5, z = 15} --// ORDER SHOULD BE Y, X, Z

getorder = function(_table)
local sortedTable = {}
for index in pairs(_table) do
table.insert(sortedTable,index)
end
return sortedTable
end

local order = getorder(myTable)
print(myTable[order[1]])
print(myTable[order[2]])
print(myTable[order[3]])

its a stupid way, quickly made it
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image