title: Most idiomatic way to sort, and then subsort nested table in lua?
body: <p>I have a table of nine {x,y} points, and I'd like to sort it first by y, then by x.  Probably best explained by my current code:</p>
  
  <pre><code>coords = {{135, 487}, {135, 298}, {334, 308}, {334, 485}, {554, 301}, {555, 400}, {136, 97}, {334, 98}, {552, 107}}
  
  table.sort(coords, function(a, b) return b[2] &gt; a[2] end)
  t1 = {}
  table.insert(t1, coords[1])
  table.insert(t1, coords[2])
  table.insert(t1, coords[3])
  t2 = {}
  table.insert(t2, coords[4])
  table.insert(t2, coords[5])
  table.insert(t2, coords[6])
  t3 = {}
  table.insert(t3, coords[7])
  table.insert(t3, coords[8])
  table.insert(t3, coords[9])
  table.sort(t1, function(a, b) return b[1] &gt; a[1] end)
  table.sort(t2, function(a, b) return b[1] &gt; a[1] end)
  table.sort(t3, function(a, b) return b[1] &gt; a[1] end)
  coords[1] = t1[1]
  coords[2] = t1[2]
  coords[3] = t1[3]
  coords[4] = t2[1]
  coords[5] = t2[2]
  coords[6] = t2[3]
  coords[7] = t3[1]
  coords[8] = t3[2]
  coords[9] = t3[3]
  
  for i, t in ipairs(coords) do
      print ("X: " .. t[1] .. " Y: " .. t[2])
  end
  </code></pre>
  
  <p>The program produces the desired output, which is this:</p>
  
  <pre><code>X: 136 Y: 97
  X: 334 Y: 98
  X: 552 Y: 107
  X: 135 Y: 298
  X: 334 Y: 308
  X: 554 Y: 301
  X: 135 Y: 487
  X: 334 Y: 485
  X: 555 Y: 400
  </code></pre>
  
  <p>So the y coordinates are grouped in threes, and within those groups of three the points are sorted by x.  The problem is I can't do a normal "sort by y, if y's are equal sort by x" because the y's aren't equal, they're just "grouped."  I'm very new to Lua, so I was wondering if there was a more idiomatic or graceful way of accomplishing this rather than what I'm currently doing?</p>
  
  <p>Thanks!</p>