Getting the "place" of something in a dictionary

``````local dictionary = {
["a"] = "hi",
["b"] = "wdwd",
["c"] = "Awaaaaaaaa",
}
``````

What if I wanted to find the place of the key “b” in the dictionary, for example, its second place in the dictionary, how would I get that?

why do you need to?
you can just reference it with the name of the value

``````local dictionary = {
["a"] = "hi",
["b"] = "wdwd",
["c"] = "Awaaaaaaaa",
}
print(dictionary.a)``````

If it was for a leaderboard, I need to know the place it is in because it is sorted from 1stplace - 10th place

You are assuming dictionaries have order. They don’t.

``````local dictionary = {
["a"] = "hi",
["b"] = "wdwd",
["c"] = "Awaaaaaaaa",
}
for key,value in pairs(dictionary)do
print(key,value)
end
23:13:50.857  a hi  -  Edit
23:13:50.857  c Awaaaaaaaa  -  Edit
23:13:50.857  b wdwd  -  Edit
``````

Vs arrays:

``````local array= {5,4,1,2}
for index,value in pairs(array)do
print(index,value)
end
--Result:
23:14:24.060  1 5  -  Edit
23:14:24.060  2 4  -  Edit
23:14:24.060  3 1  -  Edit
23:14:24.061  4 2  -  Edit
``````

You will need another method like assigning a number to the key then sorting all the occurrences of that number like this I believe:

Edit: Oh yeah thanks @itsXtimes, it’s a bad habit to put everything as i,v in pairs

i in this case will not be an int value, it will be the name of the value
for example:

``````local dictionary = {
["a"] = "hi",
["b"] = "wdwd",
["c"] = "Awaaaaaaaa",
}
for i, v in pairs(dictionary) do
print(i)
end
-- 18:20:38.543  a  -  Server - Script:7
-- 18:20:38.543  c  -  Server - Script:7
-- 18:20:38.543  b  -  Server - Script:7``````
1 Like

Dictionaries don’t order, it’s impossible to get the “place” it’s in. You would have to use an array or get around it by adding a property called “position” or something.

Edit: or as @dthecoolest posted you can put all the keys into an array

so show us this leaderboard script and we maybe find another solution

Ok hold on give me a second I will get it

Actually, I am got it, I found a solution. I had an earlier devforum post I looked at to help me with getting the sort of places, I am going to use that again to get the places, thank you for the help though!

(Should I just delete this post)