How would I sort this?

So there is many questions about this and the same answer but that answer isn’t working correctly. Example:

local Number = {1,22}

table.sort(Number, function(a,b)
    return (a>b)
end)

for i,v in pairs(Number) do
    print(v)
end

Output:

9, 8, 7, 6, 5, 4, 3, 22 ,21, 20 ,2, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 1

Wanted Output:

22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

What do I do to achieve wanted output instead of the wrong output?

3 Likes

Make sure the elements inside the table is a "number" and not a "string".
It looks like it’s sorted by A-Z/0-9 order so it suggests that you’re sorting a string.

In the example, it is numbered though?

local table = {1}

local value = 2

repeat

task.wait(0.001)

table.insert(table, value)

value = value + 1

until table[22] == 22

for i, v in ipairs(table) do
	print(v)
end

Very bad with Ipairs and Pairs so this is the best I can do, hope it helps and works!

1 Like

The difference between pairs and Iparis are that Ipairs is chronological order,

Well, I’m afraid I’m unable to reproduce then. Your desired output seems to be 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 and the example only has 22 and 1 in it so are you sure this example script is the issue?

1 Like
local Number = {}
for index = 1, 22 do
   table.insert(Number,index)
end

table.sort(Number, function(a,b)
    return (a<b) -- less than
end)

for i,v in pairs(Number) do
    print(v)
end

Ok, let me rephrase this because it appears some are confused.

I’m doing this from number 1 to number 22 in a list (In my real script it collects all the names of objects and turns them into numbers and places them in a list)

And now I have to sort them but it sorts them like Output where I want the wanted Output.

1 Like
local array = {1}

local pos = 2

local value = 2

repeat

	task.wait(0.001)

	table.insert(array, pos, value)

	value = value + 1
	
	pos = pos + 1

until array[22] == 22

for i, v in ipairs(array) do
	print(v)
end

Prints 1 to 22!

You can do reverse by setting the array first value to 22 and setting value to 21 and make the pos minus everytime instead of add!

If you’re getting the object name then it is a "string" data type regardless if the value of the string is numeric.

If the string just have digits (0-9) in it - did you try casting it to a "number" via tonumber?

No one understands so I’m just gonna go figure something out I guess.

Sir i gave you the wanted output, check it out:

local array = {1}

local pos = 2

local value = 2

repeat

	task.wait(0.001)

	table.insert(array, pos, value)

	value = value + 1
	
	pos = pos + 1

until array[22] == 22

for i, v in ipairs(array) do
	print(v)
end

Prints 1 to 22!

You can do reverse by setting the array first value to 22 and setting value to 21 and make the pos minus everytime instead of add!

That is still not what I want so just leave this topic, I’ll go figure something out :confused:

What do you want? Everyone is confused at what you want, since multiple people have gave you an answer?