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 » Scripters
Home Search
 

Re: How to get tables to be in a specific order?

Previous Thread :: Next Thread 
Quebankus is not online. Quebankus
Joined: 25 Aug 2011
Total Posts: 846
16 Oct 2016 10:35 PM
When I do that:

data = {
["a"] = nil,
["b"] = nil,
["c"] = nil,
}

It doens't show up like I want. How to fix?
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
16 Oct 2016 10:36 PM
none of your data will exist so there will be no keys .-.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
16 Oct 2016 10:37 PM
for dictionaries, you can't directly. you will need to use a custom function. For arrays however,

table.sort(array,function(a,b)
return a:lower():byte < b:lower():byte()
end)


Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
16 Oct 2016 10:38 PM
^ why do you need to use bytes?

You can just use the automatic sorting of

"string1" < "string2"

it works better and sorts based on any characters after the similar ones
Report Abuse
Quebankus is not online. Quebankus
Joined: 25 Aug 2011
Total Posts: 846
16 Oct 2016 10:48 PM
I mean it's not going to be letters.. It was just a placeholder, the real value should be human-readable names.
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
16 Oct 2016 10:54 PM
i guess you can do

function sortedpairs(table)
local keys = {}
for key,_ in next,table do
keys[#keys+1] = key
end
table.sort(keys,function(a,b)
return a:lower() < b:lower()
end)

local i = 1

return function()
return keys[key], table[key]
i = i + 1
end

end

for k,v in sortedpairs({b="there"; d="lol"; a="hi"; c="buddy"}) do
print(k,v)
end
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
16 Oct 2016 10:56 PM
oops fixed:

function sortedpairs(tbl)
local keys = {}
for key,_ in next,tbl do
keys[#keys+1] = key
end
table.sort(keys,function(a,b)
return a:lower() < b:lower()
end)

local i = 0

return function()
i = i + 1
return keys[i],tbl[i]
end

end

for k,v in sortedpairs({b="there"; d="lol"; a="hi"; c="buddy"}) do
print(k,v)
end
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
16 Oct 2016 10:57 PM
return keys[i],tbl[keys[i]]

sorry for being stupid :P

it will slow down performance though
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
16 Oct 2016 11:23 PM
Non-array tables have no order. Use an array if you want order.


Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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