Thread Tools Display Modes
02-01-15, 01:36 PM   #21
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 130
Hey that makes sense! Thanks for the help, that's exactly what I needed!
  Reply With Quote
02-01-15, 01:50 PM   #22
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 130
One more thing I forgot to ask about, is sorting numbers in a string. I can understand what it's doing, but is there a way to force it to sort the strings in normal numeric order?

Lua Code:
  1. local a = { "25","24","1","3"};
  2.  
  3. table.sort(a);
  4.  
  5. for i=1,4 do
  6.     print(a[i]);
  7. end
  8.  
  9. --Prints as:
  10. --[1] "1"
  11. --[2] "24"
  12. --[3] "25"
  13. --[4] "3"
  14.  
  15. --Want it as:
  16. --[1] "1"
  17. --[2] "3"
  18. --[3] "24"
  19. --[4] "25"

I have a feeling that this isn't possible, I just wanted to confirm
  Reply With Quote
02-01-15, 02:38 PM   #23
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
You can convert your strings to numbers before comparing them, or you could preferably just not store them as strings in the first place.
Lua Code:
  1. local t = {"25", "24", "1", "3"}
  2.  
  3. table.sort(t, function(a, b)
  4.   return tonumber(a) < tonumber(b)
  5. end)
  6.  
  7. for i = 1, 4 do
  8.   print(t[i])
  9. end

Last edited by semlar : 02-01-15 at 02:41 PM.
  Reply With Quote
02-01-15, 05:18 PM   #24
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
On the main subject:
If you want a way to access the indexes in order and you also need to add and remove indexes without losing the order then the best way is to implement a binary search tree with the key values. In a binary search tree you can add and remove elements without having the sort the table every time. I have a simple implementation with an example in this thread:
http://us.battle.net/wow/en/forum/topic/11986218743#7
My example only deal with strings but it is possible to generalize it by passing a function that is able to determine the order between indexes (like comparing a number to a string and returning 1,0,-1 depending if the first argument should be considered greater, equal or lower than the second).

Lua Code:
  1. --insert value in the binary tree
  2. function BinarySearchTree.insert(tree_node,value,compare_values)
  3.     if tree_node==nil then
  4.         tree_node = {}
  5.         tree_node.value = value
  6.         tree_node.n = 1
  7.     elseif compare_value(tree_node.value,value)>0 then
  8.         tree_node.left = BinarySearchTree.insert(tree_node.left,value,compare_values)
  9.         tree_node.n = tree_node.n+1
  10.     elseif compare_value(tree_node.value,value)<0 then
  11.         tree_node.right = BinarySearchTree.insert(tree_node.right,value,compare_values)
  12.     end
  13.     return tree_node
  14. end
  15.  
  16. --remove value from a binary tree
  17. function BinarySearchTree.delete(tree_node,value,compare_values)
  18.     if tree_node then
  19.         if compare_value(tree_node.value,value)>0 then
  20.             tree_node.left = BinarySearchTree.delete(tree_node.left,value,compare_values)
  21.             tree_node.n = tree_node.n-1
  22.         elseif compare_values(tree_node.value,value)<0 then
  23.             tree_node.right = BinarySearchTree.delete(tree_node.right,value,compare_values)
  24.         elseif tree_node.left==nil and tree_node.right==nil then
  25.             tree_node = nil
  26.         elseif tree_node.left==nil and tree_node.right then
  27.             tree_node = tree_node.right
  28.         elseif tree_node.left and tree_node.right==nil then
  29.             tree_node = tree_node.left
  30.         else
  31.             tree_node.value = BinarySearchTree.get_min_value(tree_node.right)
  32.             tree_node.right = BinarySearchTree.delete(tree_node.right,tree_node.value,compare_values)
  33.         end
  34.     end
  35.     return tree_node
  36. end
  37.  
  38. --get i-th element of the tree, m is just used on the recursion calls
  39. function BinarySearchTree.get_element_i(tree_node,i,m)
  40.     m = m or 0
  41.     if tree_node==nil then
  42.         return nil
  43.     elseif tree_node.n+m==i then
  44.         return tree_node.value
  45.     elseif tree_node.n+m>i then
  46.         return BinarySearchTree.Get_element_n(tree_node.left,i,m)
  47.     else
  48.         return BinarySearchTree.Get_element_n(tree_node.right,i,m+tree_node.n)
  49.     end
  50. end
  51.  
  52. function BinarySearchTree.get_min_value(tree_node)
  53.     if tree_node.left==nil then
  54.         return tree_node.value
  55.     else
  56.         return BinarySearchTree.get_min_value(tree_node.left)
  57.     end
  58. end
  59.  
  60. function BinarySearchTree.max_value(tree_node)
  61.     if tree_node.right==nil then
  62.         return tree_node.value
  63.     else
  64.         return BinarySearchTree.get_max_value(tree_node.right)
  65.     end
  66. end
  Reply With Quote
02-01-15, 11:29 PM   #25
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
There are other issues with this.

Code:
local tempTable = {};
tempTable = playerNames; -- The reason for this is I need "playerNames" table to remain unsorted
The above code says "The variable tempTable should hold a new, empty table." and then immediately says "Eh, forget the new empty table. Let's assign the memory address referred to by the playerNames variable to the tempTable variable instead."
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Table Sorting


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off