Home Recent changes

PICO

pico-8 cartridge http://www.pico-8.com version 7 __lua__

function sort (arr, comp)

  if not comp then
    comp = function (a, b)
      return a < b
    end
  end
  local function partition (a, lo, hi)
      pivot = a[hi]
      i = lo - 1
      for j = lo, hi - 1 do
        if comp(a[j], pivot) then
          i = i + 1
          a[i], a[j] = a[j], a[i]
        end
      end
      a[i + 1], a[hi] = a[hi], a[i + 1]
      return i + 1
    end
  local function quicksort (a, lo, hi)
    if lo < hi then
      p = partition(a, lo, hi)
      quicksort(a, lo, p - 1)
      return quicksort(a, p + 1, hi)
    end
  end
  return quicksort(arr, 1, #arr)

end

player = dex=10, played=false}, {dex=11, played=false}, {dex=12, played=true sort(player, function(a, b) return a.dex > b.dex and a.played==false end) print(player[1].dex)