Sam Trenholme's webpage
Support this website

Lua sorting again

 

September 13 2025

An update to the Lua Table sorter. Also, a website I like.

An updated Lua table sorter

Like what I posted yesterday, here is a Lua iterator which goes through elements in a table in sorted order. This routine works like the pairs() function included with Lua:

-- Like pairs() but sorted
function sPairs(inTable, sFunc)
  if not sFunc then
    sFunc = function(a, b)
      local ta = type(a)
      local tb = type(b)
      if(ta == tb)
        then return a < b 
      end
      return tostring(ta) <
             tostring(tb)
    end
  end
  local keyList = {}
  local index = 1
  for k,_ in pairs(inTable) do
    table.insert(keyList,k)
  end
  table.sort(keyList, sFunc)
  return function()
    key = keyList[index]
    index = index + 1
    return key, inTable[key]
  end
end

Example usage of the above function:

a={z=1,y=2,c=3,w=4}
for k,v in sPairs(a) do
 print(k,v)
end

With a sort function:

a={z=1,y=2,c=3,w=4}
function revS(a,b)
  return a>b
end
for k,v in sPairs(a,revS) do
  print(k,v)
end

revS (“Reverse sort”) is a reverse sorter. Note that revS assumes the elements to be sorted are all of the same type.

More discussion about 2025’s cancel culture

There is some good discussion about 2025’s cancel culture, albeit with a right-of-center bias, over at The Motte. In this week’s “Culture war” discussion, some of the discussion is about left-wing people who celebrated Charlie Kirk’s death and whether it’s OK for them to lose their job or what not because of that behavior.

The Motte is a stand alone forum which started off on Reddit but became stand alone because of concerns that Reddit was stifling frank open discussion.