0 Members and 2 Guests are viewing this topic.
I am a proud cynic.
> mytable = {"omnimaga", "pianoman", "ephan"}> -- When order alphabetically, it should be "ephan", "omnimaga", "pianoman"> table.sort(mytable)> = a[1]ephan> = a[2]omnimaga> = a[3]pianoman
> myinttable = {4,2,1,3}> table.sort(myinttable)> return myinttable[1]1> return myinttable[2]2> return myinttable[3]3> return myinttable[4]4
table.sort(table [, comp])Sort the elements of a table in-place (i.e. alter the table).Code: (Lua) [Select]> t = { 3,2,5,1,4 }> table.sort(t)> = table.concat(t, ", ") -- display sorted values1, 2, 3, 4, 5If the table has a specified size only the range specified is sorted, e.g.,Code: (Lua) [Select]> t = { 3,2,5,1,4; n=3 } -- construct a table with user size of 3> table.sort(t) -- sort will be limited by user size> = table.concat(t, ", ") -- only specified size is concatenated as well2, 3, 5A comparison function can be provided to customise the element sorting. The comparison function must return a boolean value specifying whether the first argument should be before the second argument in the sequence. The default behaviour is for the < comparison to be made. For example, the following behaves the same as no function being supplied:Code: (Lua) [Select]> t = { 3,2,5,1,4 }> table.sort(t, function(a,b) return a<b end)> = table.concat(t, ", ")1, 2, 3, 4, 5 We can see if we reverse the comparison the sequence order is reversed.Code: (Lua) [Select]> table.sort(t, function(a,b) return a>b end)> = table.concat(t, ", ")5, 4, 3, 2, 1
> t = { 3,2,5,1,4 }> table.sort(t)> = table.concat(t, ", ") -- display sorted values1, 2, 3, 4, 5
> t = { 3,2,5,1,4; n=3 } -- construct a table with user size of 3> table.sort(t) -- sort will be limited by user size> = table.concat(t, ", ") -- only specified size is concatenated as well2, 3, 5
> t = { 3,2,5,1,4 }> table.sort(t, function(a,b) return a<b end)> = table.concat(t, ", ")1, 2, 3, 4, 5
> table.sort(t, function(a,b) return a>b end)> = table.concat(t, ", ")5, 4, 3, 2, 1
Interesting... could be very useful.Thanks, ephan!
n = io.read()n = tonumber(n)f = {}for i=1,n do a = io.read() table.insert(f, a)endtable.sort(f)print("")for i=1,n do print(f[i])end