1. --[[ 
  2. 稳定的多维条件数组排序 
  3. 由调用者保证tbCondition的元素个数大于等于维度nDimension 
  4. ]] 
  5. function MutilArraySort(tbCondition, nDimension) 
  6.     local tbResult = {} 
  7.     for i = 1, #tbCondition do 
  8.         local nInsert = 0 
  9.         local nLocal = 1 
  10.         for k = 1, nDimension do 
  11.             local function DimensionCompare(j) 
  12.                 if j > #tbResult then 
  13.                     return #tbCondition + 1 
  14.                 end 
  15.                 if tbCondition[i][k] > tbCondition[tbResult[j]][k] then 
  16.                     return j 
  17.                 elseif tbCondition[i][k] == tbCondition[tbResult[j]][k] then 
  18.                     return 0, j 
  19.                 else 
  20.                     return DimensionCompare(j + 1) 
  21.                 end 
  22.             end 
  23.             nInsert, nLocal = DimensionCompare(nLocal) 
  24.             if nInsert > 0 and nInsert <= #tbResult then 
  25.                 table.insert(tbResult, nInsert, i) 
  26.                 break 
  27.             elseif nInsert > #tbResult then 
  28.                 table.insert(tbResult, i) 
  29.                 break 
  30.             end 
  31.             if k == nDimension then 
  32.                 table.insert(tbResult, i) 
  33.                 break 
  34.             end 
  35.         end 
  36.     end 
  37.     return tbResult 
  38. end 
  39.  
  40. tbCondition = { 
  41.     [1] = { 1, 2, 6, 6 }, 
  42.     [2] = { 5, 2, 8, 5 }, 
  43.     [3] = { 5, 2, 7, 6 }, 
  44.     [4] = { 5, 2, 1, 4 }, 
  45. local tbResult = MutilArraySort(tbCondition, 4) 
  46. for i = 1, #tbResult do 
  47.     print(tbResult[i]) 
  48. end