How to tell which number is higher from a dictionary?

I have this dictionary

Choices = {["I want to sell my fish! 🐟"] = 2, ["I want to buy a new rod! 🎣"] = 5}

And I wanna be able to see which is first, so I can assign it to FirstText. However, I sometimes get the second option becoming the FirstText

for text, nextQuery in pairs(TotalMessages.Choices) do
	if not FirstOption then
		FirstText = text
		FirstOption = nextQuery
	else
		SecondText = text
		SecondOption = nextQuery
	end
end

Quite don’t understand?
If you wanted to do, like first,then second, then third choice just change ‘pairs’ for ipairs

pairs: A random number.
ipairs: counts up from 1,2,3,4 etc.

I get an error then

[bad argument #3 (string expected, got nil)]

-- Setup choice boxes
local FirstText
local SecondText

for text, nextQuery in ipairs(TotalMessages.Choices) do
	if not FirstOption then
		FirstText = text
		FirstOption = nextQuery
	else
		SecondText = text
		SecondOption = nextQuery
	end
end

Choice1.Button.Title.Text = FirstText -- ERROR HERE
Choice2.Button.Title.Text = SecondText

FirstText is nil than… … . … 30CHARS

Ye ik that, but why. When I used pairs it wasn’t nil

pairs or ipairs will not affect that

Except it did. When I used pairs, it wasn’t nil. Changing to ipairs caused it to be nil

Try printing on the loop the ‘text’

Lua hash tables are not stored in the order you assign them in, meaning your "I want to buy a new rod! 🎣" text may appear first when using pairs. A better solution would be to store your data in an ordered fashion:

{
    {    --This is the first entry
        Text = "I want to sell my fish! 🐟",
        NextQuery = 2
    },
    {    --This is the second entry
        Text = "I want to buy a new rod! 🎣",
        NextQuery = 5,
    }
}

and using the ipairs method to iterate through all of the entries in order, from top to bottom:

for i,entry in ipairs(TotalMessages.Choices) do    --Will always go in order
    local text,nextQuery = entry.Text,entry.NextQuery;
    --Your code here
end
3 Likes

Not necessarily, ipairs just stops iteration when an element is nil; in the iterator function ipairs returns the value is compared to make sure it’s not nil, if it isn’t then the numerical index and value are returned.

Dictionaries are stored as hashmaps - there’s no certain order to the hashing algorithm. Pairs just returns next, t, nil , and the behavior of next is undefined even for numerically indexed tables.