Table Handler; for all your array's needs

Table Handler

The only object you would need for somewhat redundant and tedious array manipulation!

This open sourced project was created with efficiency in mind.
[Also for the sake of being able to learn and progress with my programming skills]
A successor to the previous module

This module is essentially an object (from here onwards, it will be referenced as TableHandlerObject) that is packed with methods and features that native lua lacks.
Such as slicing, concatenation just to name a few.

TableHandlerObject contains many methods, below is a list of all of them.

List of methods

  • Flipping an array
  • Slicing
  • Concatenation
  • Shifting
  • Sorting with 5 different algorithms
  • Deep sorting (sorts nested arrays); comes with all 5 sorting algorithms

List of features

  • Rather efficient.
    • Removed as many redundant and unnecessary calls possible,
    • Algorithms are streamlined to do what it has to do.
  • Great control.
    • You have great control over what’s to be done,
    • Modifying the passed array or not; your choice.
  • Properties are protected.
    • At the base level, TableHandlerObject is a protected metatable, with __metatable = "",
    • Whenever you change a property within TableHandlerObject, it will call :WriteProperties() to validate the value and then assigning it upon validation.
  • Ability to deal with non-numerical datatypes.
    • Non-number datatypes are appended into the results if .IncludeNonSorted is true,
    • The placement of the non-number datatypes are maintained amongst themselves, just grouped together and appended to the sorted list.
  • Features 5 sorting algorithms.
    • Bubble sort, insertion sort, heap sort, quick sort and selection sort,
    • Simply change the sorting algorithm by changing the .Algorithm property.
  • No dependancy on other modules or external functions (be it built-in or what not etc).
    • This single modulescript would be all you need.
    • After all, this entire module is made with runtime speed in mind.

Just the barebones of what TableHandlerObject is capable of
TableHandlerObject itself comes with multiple properties.
Such as:

Properties

  • .Ascending - to determine sorting ‘direction’
  • .Algorithm - to determine the sorting algorithm to use
  • .IncludeNonSorted - to append non-numerical elements to the end of the results (while maintaining placement)
  • .ModifyArray - whether to modify the passed array as an argument
  • .SortFirstFew - determines the first few elements to sort, a value of -1 to sort the entire passed array
  • .SortingParameters - for algorithms that have ‘customisable’ constants

Example usage:

local table_handler = require(game:GetService("ServerScriptService"):WaitForChild("TableHandler"))
local o = table_handler.new()

print(o:GetProperties()) -- {Ascending = true, Algorithm = 1, IncludeNonSorted = true, ModifyArray = false, SortFirstFew = -1, SortingParameters = {}}

local sorted = o:Sort({5, "a", 1, 2})
print(sorted) -- {1, 2, 5, "a"}

o.Ascending = false
local sorted_d = o:DeepSort({3, 4, 1, {4, 1, 10, {3, 1}}})
print(sorted_d) -- {4, 3, 1, {10, 4, 1, {3, 1}}}

local sliced = o:Slice({"a", "b", "c", "d", "e"}, 1, -2, 2) -- start: 1, stop: 2nd last element, step: 2
print(sliced) -- {"a", "c"}

Output:

{Ascending = true, Algorithm = 1, IncludeNonSorted = true, ModifyArray = false, SortFirstFew = -1, SortingParameters = {}}
{1, 2, 5, "a"}
{4, 3, 1, {10, 4, 1, {3, 1}}}
{"a", "c"}

Read a more detailed documentation at the GitHub repository

I’ve listed and documented all the methods and its behaviours in it.
Most updated code can be found there.

  • Grab the roblox object here

Please do report any bugs you find, will be eternally grateful and will fix them as fast as possible.
Thanks for reading!

~ Faded

9 Likes

Thanktastic.

I was having issues with tables a week back and this would have come in very handy!
Might implement it tonight.

Thanks for sharing!

Thanks!
Do let me know if you are encountering any issues when implementing, I would be glad to help.