Table Sorting; Both arrays and dictionaries' keys or values

Module that fills all your sorting needs. :")
Note that it was only until completion that I’ve discovered that table.sort() exists.
Regardless, I believe that the module I am going to share would surpass table.sort() in terms of functionality as it features “deep sorting”.


Features

  • Ability to sort both dictionaries’ values/keys and arrays.
    • It is able to sort either the dictionary’s keys or its values.
    • Though when sorting through a dictionary, it will be returned as an array with the sorted elements as dictionaries are unordered, unlike arrays.
    • So it would be impossible to sort dictionaries.
  • Arrays passed as parameters won’t be changed within the module.
    • A new array with a new memory address will be returned.
  • Deep sorting.
    • Relies on recursion to sort through all nested arrays.
  • Ability to deal with non numerical datatypes.
    • When dealing with non numerical datatypes, it will either be excluded or if IncludeNonSorted is enabled, it will be pushed to the back, including nested arrays when using :Sort().
  • Features 2 sorting algorithms as of now, heap sort and insertion sort.

Read more on its features and API/how to use on the github repo here:

Grab the module here:


API


Module.new()

Creates and return a new SorterObj.

Objects


  • SorterObj

    A class object that handles parameters when sorting.

    Methods

    :Sort(x)

    • Sorts array x, will exclude non numeric datatypes, use Deep Sort to include arrays.
    • Only takes in 1 parameter.
    • No changes will be made to x within the module.
    • Skips nil, won’t be added into returned list regardless of IncludeNonSorted.

    :DeepSort(x)

    • Sorts array x along with nested arrays.
    • Uses a recursive function to sort all of the numerical data within all of nested arrays.
    • All nested arrays would be pushed to the very back.
    • Won’t sort non numeric datatypes.
    • Only takes in 1 parameter.
    • No changes will be made to x within the module.
    • Skips nil, won’t be added into returned list regardless of IncludeNonSorted.
    • Does not support dictionary input.

    Parameters

    Algorithm

    Datatype: number
    Default: 1
    Determines the sorting algorithm used.
    1 - Heap Sort
    2 - Insertion Sort

    MaxRetries

    Datatype: number
    Default: 5
    Determines the amount of times to retry when checking of ordered array fails.

    Checks

    Datatype: boolean
    Default: true
    Determine whether to check the sorted array.
    Uses MaxRetries

    Ascending

    Datatype: boolean
    Default: true
    Whether to sort the given array in ascending order.

    Type

    Datatype: number
    Default: 1
    Type of table, 1 – array; 2 – dictionary.

    SortKeys

    Datatype: boolean
    Default: false
    Only applicable when sorting a dictionary,
    Instead of sorting the values of the dictionary, it sorts the keys.

    IncludeNonSorted

    Datatype: boolean
    Default: false
    When set to true, it will pack all of the non numerical datatypes from the given list at the very end of the returned list.


Sorting Stats

Algorithm 1; Heap Sort

  • Avg time: 0.03131ms

Algorithm 2; Insertion Sort

  • Avg time: 0.03652ms

Code samples

  • Creating the Sorter Object
local sorter = require(script.Parent:WaitForChild("ModuleScript"))

SorterObj = sorter.new() -- get the sorter object
-- customise parameters
-- note: no need to assign them as they have default values
SorterObj.Ascending = true
SorterObj.Algorithm = 1
SorterObj.Type = 1

  • :Sort()
local array = {3, 1, 19, 15}
local sortedArray = SorterObj:Sort(array)
print(sortedArray) -- {1, 3, 15, 19}

SorterObj.Ascending = false
sortedArray = SorterObj:Sort(array)
print(sortedArray) -- {19, 15, 3, 1}

array = {3, "A", 19, "C", 5}
sortedArray = SorterObj:Sort(array)
print(sortedArray) -- {3, 5}

  • IncludeNonSorted
SorterObj.IncludeNonSorted = true

local array = {1, "test", 3, "B", 1, 3, 0} 
local sortedArray = SorterObj:Sort(array)
print(sortedArray) -- {0, 1, 1, 3, 3, "test", "B"}

array = {1, "test", {9, 4, 10}, "B", 8, 3}
sortedArray = SorterObh:Sort(array)
print(sortedArray) -- {1, 3, 8, "test", {9, 4, 10}, "B"}

  • :DeepSort()
SorterObj.Ascending = true
SorterObj.Algorithm = 1
SorterObj.Type = 1

local array = {1, 30, 41, 3, {3, 34, 1, 0, {5, 64, 10}}, {10, 31, 05, 10, 11}}
local sortedDeepArray = SorterObj:DeepSort(array)
print(sortedDeepArray) -- {1, 3, 30, 41, {0, 1, 3, 34, {5, 10, 64}}, {5, 10, 10, 11, 31}}
-- all nested arrays would be pushed to the very end in order

Error Codes

For your troubleshooting needs.
108 - Invalid SorterObj’s parameters.
350 - Not a fatal error, provided type property of SorterObj does not match the given table.
349 - SorterObj.Type is not a valid option.
801 - Amount of checks reached MaxRetries. Contact me if this happens.

Please note that this is the first time I am sharing something within community resources, thank you for your understanding.

Feedback would be most appreciated!
^ _ ^

5 Likes