Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Greatest to Least without table.sort()?

If I gathered numbers within a table or without and need them greatest to least without using table.sort().

4 comments

0

You could implement it yourself. But why can't you use table.sort?

0

My table uses multiple values within an value.

0

Why can't you use table.sort for that...?

0

Trying to sort 'table.insert(Items, {Item.id, Item.name, Item.rap, #Item.userAssetId})' is tricky. "table.sort(Items, function(a, b) return a > b end)"

1 answer

If you need a list sorted, it doesn't make sense to reinvent what's already been given to you. Sorting is moderately difficult to get right, and in any case you will need to implement the comparison function you'd be giving to table.sort in the first place.

If you wanted to sort a list of these items by their .rap, you could use something like this:

function descendingRAP(a, b)
	return a.rap > b.rap
end

table.sort(Items, descendingRAP)

Another option, instead of using table.sort, you can just insert the item into the proper place:

function add(element)
	for i = 1, #list do
		if element < list[i] then
			table.insert(list, i, element)
			return
		end
	end
	table.insert(list, element)
end

where element < list[i] could be replaced by any less(element, list[i]), where less is the function you would pass as the second parameter to table.sort.

1 comment

0

I asked you this exact question a while back, and I was about to give the answer you gave me but you beat me to the chase xD

This discussion is preserved as it was. New answers, comments, and votes are closed.