• Subject: Mixed type list processing
• From: "Tony Bedford" <bedford@...>
• Date: Tue, 16 Jul 2002 16:54:52 +0100

```Hi,

I'm new to the list and have just written my first Lua program (see below).
My question is I am sorting a list with different types, and I
have a solution that works below, but I am sure there is a much better
way. Can anyone offer a neater solution please? The main problem
is getting the compare neat, as you have to compare a number to
a string when sorting the list.

Thanks,
Tony

-- globals
true = 0
false = -1
length = 0

-- print_item and count items in list
function print_item(i,x)
print (i,x)
length = length + 1 -- I don;t like doing this
end

-- check_item
function check_item (a,b)
a = strlower(a)
b = strlower(b)
a = tostring (a)
b = tostring (b)
return a, b
end

-- sort_list
function sort_list (list,n)
local sorted = true
while sorted == true do
sorted = false
for i=1,n-1,1 do
a, b = check_item (list[i],list[i+1])
if a>b then
list[i],list[i+1] = list[i+1],list[i] -- swap
sorted = true
end
end
end
end

-- list = {3,7,4,2.1,3.1416,9,-1,0}
-- list = {"cat","dog","bird","shark","bat","mouse"}
local list = {"cat",7,"ant",4,2,"bird","Bat","Duck"}

foreachi (list, print_item)
sort_list (list,length)
print ("Sorted list:")
foreachi (list, print_item)

----------------------------------------------------------------------------
-

The contents of this e-mail and any attachments are confidential and may
be legally privileged. If you have received this e-mail and you are not