• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Function getTableInAlphabeticalOrder

MaXwEllD

New Member
Joined
Sep 10, 2009
Messages
28
Reaction score
1
Location
Brazil
Author: MaXwEllDeN(I'm)
Hi guys :w00t:

I have a little and simple function that i made '-'

Code:
function getInAOrder(tabl)
   local dat = {}
   local tab = {}
   local tab2 = {["A"] = 1, ["Ä"] = 1, ["Ã"] = 1, ["Â"] = 1, ["À"] = 1, ["Á"] = 1, ["B"] = 2, ["C"] = 3, ["D"] = 4,
   ["E"] = 5, ["Ë"] = 5, ["Ê"] = 5, ["À"] = 5, ["F"] = 6, ["G"] = 7, ["H"] = 8, ["I"] = 9, ["Ï"] = 9, ["Ì"] = 9, 
   ["Í"] = 9, ["Î"] = 9,["J"] = 10, ["K"] = 11, ["L"] = 12, ["M"] = 13, ["N"] = 14, ["Ñ"] = 14, ["O"] = 15, 
   ["Ö"] = 15, ["Ó"] = 15, ["Ò"] = 15, ["Ô"] = 15, ["Õ"] = 15,["P"] = 16, ["Q"] = 17, ["R"] = 18, ["S"] = 19, 
   ["T"] = 20, ["U"] = 21, ["V"] = 22, ["W"] = 23, ["X"] = 24, ["Y"] = 25, ["Z"] = 26 }

   for a = 1,26 do
       table.insert(tab, {})
   end                                 
   for a, b in pairs(tabl) do                      
    if (tab2[b:sub(1, 1):upper()]) then   
       table.insert(tab[tab2[b:sub(1, 1):upper()]], b)
    else
        table.insert(tab[#tab2], b)   
    end
   end
   for a, b in ipairs(tab) do       
      for c, d in ipairs(b) do
          table.insert(dat, d)
      end
   end
      
   return dat                                                 
end

What this function make is organize an table in the alphabetical order. If you run this:

Code:
   local t = {"Socket", "Lua", "C++", "Linux", "windows", "ubuntu", "C", "Delphi", "Alfa", "Ômega", "PHP", "HTML"}
   for a, b in pairs(getInAOrder(t)) do
      print(b)
   end

Will return this:

Code:
Alfa
C++
C
Delphi
HTML
Lua
Linux
Ômega
PHP
Socket
ubuntu
windows

Sorry my bad english. :/
 
I think the borest part of the function is:
local tab2 = {["A"] = 1, ["Ä"] = 1, ["Ã"] = 1, ["Â"] = 1, ["À"] = 1, ["Á"] = 1, ["B"] = 2, ["C"] = 3, ["D"] = 4,
["E"] = 5, ["Ë"] = 5, ["Ê"] = 5, ["À"] = 5, ["F"] = 6, ["G"] = 7, ["H"] = 8, ["I"] = 9, ["Ï"] = 9, ["Ì"] = 9,
["Í"] = 9, ["Î"] = 9,["J"] = 10, ["K"] = 11, ["L"] = 12, ["M"] = 13, ["N"] = 14, ["Ñ"] = 14, ["O"] = 15,
["Ö"] = 15, ["Ó"] = 15, ["Ò"] = 15, ["Ô"] = 15, ["Õ"] = 15,["P"] = 16, ["Q"] = 17, ["R"] = 18, ["S"] = 19,
["T"] = 20, ["U"] = 21, ["V"] = 22, ["W"] = 23, ["X"] = 24, ["Y"] = 25, ["Z"] = 26 }
 
use table.sort :s
 
these regional characters won't sort properly, but nobody uses them anyway :p:p:p
 
Back
Top