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 |
|
|
|
| 16 Oct 2016 10:36 PM |
| none of your data will exist so there will be no keys .-. |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|