Thread Tools Display Modes
04-25-08, 06:13 AM   #1
jaliborc
A Chromatic Dragonspawn
 
jaliborc's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 196
Shorting table values by alphabetic order

How can I short table values by alphabetic order? I want to save values with a given id, and then short them by id alphabetic order. Here is a example:

Code:
local table = {}

local function addLine(id, value)
  table[id] = value
end

addLine('Apple', {name = 'CrazyApple', otherValue = 'example'}
addLine{'Banana', {name = 'StupidBanana', someValue = 'example'}
addLine{'Strawberry', {name = 'Mania Strawberry', oneValue = 'example'}

for k,v in pairs(table) do
  --some function
end
The problem is with the last three lines. I'm sure there is a way to short the values by the correct order, but I don't know how.
  Reply With Quote
04-25-08, 06:29 AM   #2
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
By short do you mean sort?
  Reply With Quote
04-25-08, 07:39 AM   #3
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
local function f (a, b)
--return true if a comes before b alphabetically
end

table.sort(table, f)
  Reply With Quote
04-25-08, 07:53 AM   #4
jaliborc
A Chromatic Dragonspawn
 
jaliborc's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 196
Originally Posted by Slakah View Post
By short do you mean sort?
Yes, sorry, I'm not a native English speaker.

Originally Posted by Akryn View Post
local function f (a, b)
--return true if a comes before b alphabetically
end

table.sort(table, f)
I will try that. Thanks!

EDIT: How can I know if a comes before b? Would using strbyte work?
  Reply With Quote
04-25-08, 08:56 AM   #5
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
yeah -- i've never had to do this in lua, but here's what i came up with off the top of my head (untested)
Code:
local function f(a, b)
    a, b = strlower(a), strlower(b)
    for i = 1, strlen(a) do
        local c1, c2 = strbyte(a, i), strbyte(b, i)
        if c1 and c2 then
             if c1 ~= c2 then
                 return c1 < c2
             end
        elseif c1 then
             return false
        end
    end
end
  Reply With Quote
04-25-08, 09:24 AM   #6
Layrajha
A Frostmaul Preserver
 
Layrajha's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 275
I haven't tested it, but I'd assume that the basic "<" operator is the alphabetical order, when used to compare strings. Thus, doing

Code:
table.sort(table)
would work, or if you want to use table.sort(table, f), you could define

Code:
function f(a,b)
  return a<b
end
or

Code:
function f(a,b)
  if a<b then
    return true
  else 
    return false
end
(but it's ugly)
  Reply With Quote
04-25-08, 03:51 PM   #7
jaliborc
A Chromatic Dragonspawn
 
jaliborc's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 196
Originally Posted by Layrajha View Post
I haven't tested it, but I'd assume that the basic "<" operator is the alphabetical order, when used to compare strings. Thus, doing

Code:
table.sort(table)
would work, or if you want to use table.sort(table, f), you could define

Code:
function f(a,b)
  return a<b
end
I think Lua can't compare string values with "<". strbyte is required to turn them in numbers
  Reply With Quote
04-25-08, 03:53 PM   #8
jaliborc
A Chromatic Dragonspawn
 
jaliborc's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 196
Originally Posted by Akryn View Post
yeah -- i've never had to do this in lua, but here's what i came up with off the top of my head (untested)
Code:
local function f(a, b)
    a, b = strlower(a), strlower(b)
    for i = 1, strlen(a) do
        local c1, c2 = strbyte(a, i), strbyte(b, i)
        if c1 and c2 then
             if c1 ~= c2 then
                 return c1 < c2
             end
        elseif c1 then
             return false
        end
    end
end
Thanks again! I already have an idea how to do it. It should be close to your code with some minor changes.
  Reply With Quote
04-27-08, 03:13 AM   #9
Layrajha
A Frostmaul Preserver
 
Layrajha's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 275
Originally Posted by jaliborc View Post
I think Lua can't compare string values with "<". strbyte is required to turn them in numbers
Just tested it because I was almost sure that it worked, and indeed, it works
  Reply With Quote
04-27-08, 03:20 AM   #10
jaliborc
A Chromatic Dragonspawn
 
jaliborc's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 196
Originally Posted by Layrajha View Post
Just tested it because I was almost sure that it worked, and indeed, it works
Even better!

Last edited by jaliborc : 04-27-08 at 03:26 AM.
  Reply With Quote
04-29-08, 11:23 AM   #11
jaliborc
A Chromatic Dragonspawn
 
jaliborc's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 196
I don't know why, but the function "sort" isn't working. Calling it is the same as doing nothing. The #2 arg (a function) is not even called.

EDIT: Appears that the sort function only works with number indexes. I was looking for some way to sort the table with string indexes. Any ideas?

Last edited by jaliborc : 04-29-08 at 11:26 AM.
  Reply With Quote
04-29-08, 11:46 AM   #12
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Originally Posted by jaliborc View Post
I don't know why, but the function "sort" isn't working. Calling it is the same as doing nothing. The #2 arg (a function) is not even called.

EDIT: Appears that the sort function only works with number indexes. I was looking for some way to sort the table with string indexes. Any ideas?
what functionality are you looking for? string-indexed tables are unsorted almost by definition, since the keys themselves aren't logically sequential. if you're looking to sort the *keys* so that foreach or pairs gives them to you alphabetically, that's not going to happen because those functions go by the actual lua back-end table order, which may as well be random.

the best thing to do may be to get rid of the string keys, replace the current values with tables in the form {"old key", old value}, and then just sort it by "old key"
  Reply With Quote
04-29-08, 11:59 AM   #13
jaliborc
A Chromatic Dragonspawn
 
jaliborc's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 196
Originally Posted by Akryn View Post
if you're looking to sort the *keys* so that foreach or pairs gives them to you alphabetically, that's not going to happen because those functions go by the actual lua back-end table order, which may as well be random.
That's why I created this thread: to know if anyone knew a way (complex or not) to short them by key values in alphabetic order.
  Reply With Quote
04-29-08, 12:44 PM   #14
jaliborc
A Chromatic Dragonspawn
 
jaliborc's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 196
Thanks guys, but I already found a way to sort the values in alphabetic order. Is a bit complex, but it works.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Shorting table values by alphabetic order


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