WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Order by name (https://www.wowinterface.com/forums/showthread.php?t=49884)

Tntdruid 09-13-14 11:04 AM

Order by name
 
1 Attachment(s)
Can't find a way to order it by name.

See screen.


Link to the code https://github.com/Tntdruid/Accounta...Accountant.lua


Trying to update one of my fav old addons :D

Xrystal 09-13-14 01:46 PM

I haven't tested this but it is a variant of one that was listed at : http://forums.coronalabs.com/topic/4...e-on-2-values/


Lua Code:
  1. -- Sort Function
  2. local function SortTableByKey(a, b )
  3.   if (a.key < b.key) then
  4.     return true
  5.   elseif (a.key > b.key) then
  6.     return false
  7.   end
  8. end
  9.  
  10. -- Build table that can be sorted
  11. local accountant_sorted_data = {}
  12. for key,value in pairs(Accountant_Data) do
  13.   table.insert(accountant_sorted_data = { "Key" = key, "Value" = value })
  14. end
  15.  
  16. -- Sort Table
  17. table.sort( accountant_sorted_data, SortTableByKey)

Tageshi 09-13-14 02:43 PM

Another example, where I assumed you want to sort Accountant_Data with its tiltles.

Lua Code:
  1. Accountant_Data = {
  2.    ["TRAIN"] = {title = "ACCLOC_TRAIN", somedata = "foo"},
  3.    ["TAXI"] =  {title = "ACCLOC_TAXI", somedata = "bar"},
  4.    ["TRADE"] = {title = "ACCLOC_TRADE", somedata = "something"},
  5.    ["AH"] =    {title = "ACCLOC_AUC", somedata = "something"}
  6. };
  7.  
  8. local sorted_keys = {};
  9. local idx = 0;
  10. for k,v in pairs(Accountant_Data) do
  11.    idx = idx + 1;
  12.    sorted_keys[idx] = k;
  13. end
  14.  
  15. table.sort(sorted_keys, function (fst, snd)
  16.       return Accountant_Data[fst].title < Accountant_Data[snd].title;
  17. end);
  18.  
  19. for idx = 1, #sorted_keys do
  20.    local key = sorted_keys[idx];
  21.    print(Accountant_Data[key].title);
  22. end

Tntdruid 09-14-14 01:44 AM

Thanks , time to see if i can get it too work :)


All times are GMT -6. The time now is 02:29 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI