#table only returns the number of elements in a sequenced index. So if you have something at 1 - 5 & 35, #table is going to return 5 not 6.
Not true, the behavior of #table is considered 'undefined' in the case that the table is anything other than a continuous array of integers without any gaps. It could return 35 in that case or it could return 5.
This is why ipairs() should always be used over 'for i = 1, #table' in iterators if you are expecting the same behavior as ipairs but can not fully trust the source 100% because you did not create the table yourself.
You do this by writing your own sort function (when swapping 2 entries, you perform the swap in both tables). Here, I mean writing the actual sort function, not the comparison function. Shell sort is almost as efficient as quick sort in Lua.
Another more simple option is to just use the original hash table and an additional array that uses the keys from the hash table as its value to act as a sorted reference to the hash table. You use the array first to get the sorted keys but when you need the values you just look them up from the has table. You do have to iterate over the entire hash table though when you want to delete an entry and you can't have more than one entry with the same key like you could using two arrays.