View Single Post
04-13-06, 09:33 PM   #9
SlackerJer
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 6
I think I saw it posted above in some form, but here's the standard function I use when I need to iterate a table that's sorted by it's keys. You can use it in place of your normal pairs() function if you need it. Note that it does create another table to sort. So it's probably not something you'd want to run every .1 seconds on a large table.

Code:
function PairsByKeys(t, f)
	-- t = table
	-- f = optional sort function
	local a = {}
	for n in pairs(t) do table.insert(a, n) end
	table.sort(a, f)
	local i = 0	  -- iterator variable
	local iter = function ()   -- iterator function
		i = i + 1
		if a[i] == nil then return nil
		else return a[i], t[a[i]]
		end
	end
	return iter
end
  Reply With Quote