Skip to content
everloot

Lore archive · Warcraft Wiki, 2023 dump

Orderedpairs

Last wiki revision 2018-11-26

Iterates over the key/values in a hash table in an order defined by a key comparison function. for k, v in orderedpairs(tab[, comparisonFunc] ) do

Note that this function involves creating and sorting a temporary table.

  • tab : Table - Table to iterate over
  • comparisonFunc : Optional function - Function that, given two keys, returns whether should be returned before.

Consider a table with mostly random string keys, and values set to 1 for simplicity: animals = { aardvark = 1, Eagle = 1, zebra = 1 } You could use orderedpairs to iterate over the table in order specified by the keys: for animal, value in orderedpairs(animals) do print(animal) end -- prints Eagle, aardvark, zebra Notably, Eagle appears first, as upper-case letters appear before lower-case letters in ASCII. To avoid this, you could supply a case-insensitive comparison function: function caseInsensitiveCompare(a, b) return a:lower < b:lower end for animal, value in orderedpairs(animals, caseInsensitiveCompare) do print(animal) end -- prints aardvark, Eagle, zebra

local function orderednext(t, n) local key = t[t.__next] if not key then return end t.__next = t.__next + 1 return key, t.__source[key] end function orderedpairs(t, f) local keys, kn = {__source = t, __next = 1}, 1 for k in pairs(t) do keys[kn], kn = k, kn + 1 end table.sort(keys, f) return orderednext, keys end

Source

https://warcraft.wiki.gg/wiki/Orderedpairs · retrieved 2023-11-13 · CC BY-SA 4.0

Converted from wikitext with templates, tables and images removed, so some sentences may be missing values.

This archive predates WoW: Forever. For collected gameplay facts see the Forever database. Search the archive.