How to sort table by index

Sounds like a stupid question, but once again roblox surprised me by giving me this output:

Screen Shot 2020-07-24 at 20.09.26

as a result of this code:

So my question is, how can one sort a table by index

(Indexes are being stored as int)

How im storing my data into the table

1 Like

Can you clarify in what way you want to sort it and how it is sorted? I am not sure what “by index” means in this case.

I am not sure if table.sort is what you are looking for. It sorts a table by giving a function two arguments (commonly called a and b in parameters) that waits for a conditional return value.

I’m not exactly sure what you mean. The index is the position of an item in a table, therefore a table is always sorted by index. The “1”, “2”, “3”,“4”,“5” that it printed are the indexes and they are in order.

If you iterate with ipairs rather than pairs the array elements should be printed in the right order.

@ArtFoundation examples of index

[‘Sofa’] = 5
[‘Magic’] = 10

In these scanerios sofa and magic are the index.

The whole point of this post is roblox isn’t behaving how I would epexct and is iterating through my table as 1,2,3,5,4

@Blokav tried ipairs but It just completely broke it only relaying the first part of the table, then again I may not be using it properly since ive always avoided it :grimacing:

I know what an index is, I am assuming you want to make it a correct array (1, 2, 3, 4, 5) then as I had to read the replies.

Try using the numeric iterator (for i = 1, #t do) to make your array ordered for any differences.

This yielded the same output as running an ipairs loop.

It looks like your table is an array of strings, you cannot use pairs as there is no key, or rather the key is the index of the array but it’s not assigned to a string key. ipairs is best for these types of tables.

As everyone here, i’m not sure what you want sorted, there doesn’t seem to be any data that can be sorted in the string. Unless you want the “hi” part to be sorted?

The index of my table, as I have already stated is being stored as an int, the string is the value.

if it were a string this would error.

Oh, then in that case you can use

table.sort(yourTable, function(a,b) return a < b end)

I believe this only sorts the value, regardless I did test it and it yeiled the same result without that sort function

Oh pft silly me, i just remembered, you can’t order a dictionary, only arrays. So you’d have to loop through the list and save the keys into an array and then sort them and then use that to access the values in the dictionary.

I should have been clearer. ipairs only works when the table values are indexed with integers starting at 1, and going up by one. If you table has mixed types of indices, ipairs will only traverse the values with numbered indices. In that case the only way to iterate through all values would be to use pairs and that doesn’t guarantee the order they will be traversed.

Avoided it why? It’s clearly the better alternative to pairs since it guarantees order and just has to be used with arrays (unless you’ve got a good reason otherwise).

If your table is numerically indexed, you could loop through ipairs since its iterator function allows you to iterate over tables with integral and incremental indices (first index being 1, not 0), it might’ve apparently broken due to your table not being completely numerically indexed, same case with a numerical loop.

If you’re using a dictionary, convert it to an array to use table.sort since the table library is not relevant to dictionaries or mixed tables - or if you manage to convert it to an array simply use ipairs and iterate.

-- example of what I meant by "convert it to an array"
local d  = {
    sofa = "",
    tab = "",
}

for k, v in pairs(d) do print(k, v) end -- arbitrary order since dictionaries are implemented as hashmaps
--

local objects ={
    {Name = "sofa"},
    {Name = "tab"},
}
for _, item in ipairs(objects) do
    print(item.Name) -- prints in expected order
end

or you could just keep a separate table for the things you need in order, keep that one numerically indexed.

Or to word differently, ipairs works for any table that has incremental integers as indexes - just that it’ll ignore indexes below 1 (it will still iterate over indexes whose value >= 1) .

1 Like
local t = {[1]="asd",[3]="1asd",[4]="asdas"}; 
print("Unordered"); 
for k, v in pairs(t) do 
    print(k,v) 
end; 
c={}; 
for k in pairs(t) do 
    table.insert(c,k) 
end; 
table.sort(c,function(a,b) 
    return a < b 
end); 
print("Ordered")  
for k, v in pairs(c) do 
    print(v,t[v]) 
end

Produces the following output:

Or you could just remove the custom predicate function since that’s exactly what the default function does

== table.sort(c)
2 Likes

To make it easier for them to alter the sorting, they could want the sorted list in descending order.

1 Like

Screen Shot 2020-07-24 at 21.01.54

its chopping off half the table
Now im getting something even more random