Thread Tools Display Modes
06-21-13, 02:07 PM   #1
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Sorting

So i have a table like this:

Code:
        local Position = { }
	for i = 1, 5 do
		Position[i] = {Texture = nil, Offset = nil}
		Position[i].Texture = i
		Position[i].Offset = lolobject[i]:GetLeft()
		print(Position[i].Texture, Position[i].Offset)
	end
Is there any way i can sort it based on Offset, without the hard way?
  Reply With Quote
06-21-13, 03:09 PM   #2
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Resike View Post
So i have a table like this:

Code:
        local Position = { }
	for i = 1, 5 do
		Position[i] = {Texture = nil, Offset = nil}
		Position[i].Texture = i
		Position[i].Offset = lolobject[i]:GetLeft()
		print(Position[i].Texture, Position[i].Offset)
	end
Is there any way i can sort it based on Offset, without the hard way?
Code:
local function SortByOffset(a, b)
    return a.Offset < b.Offset
end

        local Position = { }
	for i = 1, 5 do
		Position[i] = {Texture = nil, Offset = nil}
		Position[i].Texture = i
		Position[i].Offset = lolobject[i]:GetLeft()
		print(Position[i].Texture, Position[i].Offset)
	end

        table.sort(Position, SortByOffset)
__________________
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
06-21-13, 03:20 PM   #3
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Torhal View Post
Code:
local function SortByOffset(a, b)
    return a.Offset < b.Offset
end

        local Position = { }
	for i = 1, 5 do
		Position[i] = {Texture = nil, Offset = nil}
		Position[i].Texture = i
		Position[i].Offset = lolobject[i]:GetLeft()
		print(Position[i].Texture, Position[i].Offset)
	end

        table.sort(Position, SortByOffset)
Awsome thanks, i managed to do it in another but very similar way, but i like to name the variables inside tables so i'll stick with this one. But i'll post mine too, maybe someone can use it.

Code:
        local Position = { }
	for i = 1, 5 do
		Position[i] = { }
		Position[i][1] = i
		Position[i][2] = lolobject[i]:GetLeft()
	end
	table.sort(Position, function(a, b) return a[2] > b[2] end)
        for k, v in ipairs(Position) do
		print(v[1], v[2])
	end
  Reply With Quote
06-21-13, 03:23 PM   #4
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
You'll note that I declared my sort function outside the scope of the table.sort() call to avoid re-creating the function every time I needed to sort the table.
__________________
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
06-21-13, 04:05 PM   #5
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Torhal View Post
You'll note that I declared my sort function outside the scope of the table.sort() call to avoid re-creating the function every time I needed to sort the table.
Yep i'll do that too later, since i'm gonna call that comparing function more time.
  Reply With Quote
06-22-13, 04:46 AM   #6
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Torhal View Post
You'll note that I declared my sort function outside the scope of the table.sort() call to avoid re-creating the function every time I needed to sort the table.
If i call the function from the talbe.sort like that, it doesn't pass the "a, b" arguments. Also tried it with:
Code:
table.sort(Position, SortByOffset(a,b))
same stuff.
  Reply With Quote
06-22-13, 10:38 AM   #7
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
Originally Posted by Resike View Post
If i call the function from the talbe.sort like that, it doesn't pass the "a, b" arguments. Also tried it with:
Code:
table.sort(Position, SortByOffset(a,b))
same stuff.
That would be an invalid syntax and you'd get an error, use this:

table.sort(Position, SortByOffset)
  Reply With Quote
06-22-13, 11:30 AM   #8
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
Originally Posted by Resike View Post
If i call the function from the talbe.sort like that, it doesn't pass the "a, b" arguments. Also tried it with:
Code:
table.sort(Position, SortByOffset(a,b))
same stuff.
You're passing the result of the function "SortByOffset" on two variables(a and b), which are probably nil because they are not set in that scope. In addition, table.sort will throw an error if a and b do exist because table.sort expects the variable to be a function to run for each comparison.
  Reply With Quote
06-22-13, 12:00 PM   #9
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by p3lim View Post
That would be an invalid syntax and you'd get an error, use this:

table.sort(Position, SortByOffset)
I also tried that but didn't worked, however is't working now, not sure what was wrong with it in the first place.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Sorting

Thread Tools
Display Modes

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