Lua - Sorting Lists
In Lua, we can sort a linked list easily. We can use inbuilt function table.sort() which accepts a table and sort the table. We need to convert the linked list to an array, sort the same using table.sort and then rebuild the linked list from the array as shown in steps below:
Get an Array from the List
Iterate through each entry of the list and store each value into the array.
-- convert list to an array
function list:toArray()
local array = {}
local current = self.first
while current do
table.insert(array, current[1])
current = current._next
end
return array
end
Sort the array using table.sort() function
-- get array from the list local arr = l:toArray() -- sort the array table.sort(arr)
Rebuild the list using sorted array
-- create a new list
l = list()
-- push the sorted entries into the list
for _, v in ipairs(arr) do
l:push({v})
end
Complete Example - Sorting the linked List
In below example, we're sorting the linked list using above steps.
main.lua
-- List Implementation
list = {}
list.__index = list
setmetatable(list, { __call = function(_, ...)
local t = setmetatable({ length = 0 }, list)
for _, v in ipairs{...}
do t:push(v)
end
return t
end })
-- push an element to the end of the list
function list:push(t)
-- move till last node
if self.last then
self.last._next = t
t._prev = self.last
self.last = t
else
-- set the node as first node
self.first = t
self.last = t
end
-- increment the length of the list
self.length = self.length + 1
end
-- iterate through the list
local function iterate(self, current)
if not current then
current = self.first
elseif current then
current = current._next
end
return current
end
function list:iterator()
return iterate, self, nil
end
-- convert list to an array
function list:toArray()
local array = {}
local current = self.first
while current do
table.insert(array, current[1])
current = current._next
end
return array
end
-- create a new list with values
local l = list({ "E" },{ "C" }, { "D" },{ "A" } , {"B"})
print("Original List")
-- iterate throgh entries
for v in l:iterator() do
print(v[1])
end
-- get array from the list
local arr = l:toArray()
-- sort the array
table.sort(arr)
-- create a new list
l = list()
-- push the sorted entries into the list
for _, v in ipairs(arr) do
l:push({v})
end
-- print the sorted list
print("Sorted List")
-- iterate throgh entries
for v in l:iterator() do
print(v[1])
end
Output
When we run the above code, we will get the following output−
Original List E C D A B Sorted List A B C D E
Complete Example - Sorting the linked List in descending Order
In below example, we are sorting the linked list in reverse order using same approach. Now table.sort() is used to sort the array in descending order.
main.lua
-- List Implementation
list = {}
list.__index = list
setmetatable(list, { __call = function(_, ...)
local t = setmetatable({ length = 0 }, list)
for _, v in ipairs{...}
do t:push(v)
end
return t
end })
-- push an element to the end of the list
function list:push(t)
-- move till last node
if self.last then
self.last._next = t
t._prev = self.last
self.last = t
else
-- set the node as first node
self.first = t
self.last = t
end
-- increment the length of the list
self.length = self.length + 1
end
-- iterate through the list
local function iterate(self, current)
if not current then
current = self.first
elseif current then
current = current._next
end
return current
end
function list:iterator()
return iterate, self, nil
end
-- convert list to an array
function list:toArray()
local array = {}
local current = self.first
while current do
table.insert(array, current[1])
current = current._next
end
return array
end
-- create a new list with values
local l = list({ "E" },{ "C" }, { "D" },{ "A" } , {"B"})
print("Original List")
-- iterate throgh entries
for v in l:iterator() do
print(v[1])
end
-- get array from the list
local arr = l:toArray()
-- sort the array in descending order
table.sort(arr, function(v1, v2)
return v1 > v2
end)
-- create a new list
l = list()
-- push the sorted entries into the list
for i, v in ipairs(arr) do
l:push({v})
end
-- print the sorted list
print("Sorted List")
-- iterate throgh entries
for v in l:iterator() do
print(v[1])
end
Output
When we run the above code, we will get the following output−
Original List E C D A B Sorted List E D C B A
Advertisements