Action Block to sort an array

Dm114

Well-known member
Simple and robust Action Block made to sort an array by 2 criteria:
- Alphabetical (default)/Numerical order*
- Increasing (default)/Decreasing order

INPUT:
- ArrayIN (compulsory) = the one-level array to be sorted
- DecreasingOrder = boolean variable set to False by default
- NumericalOrder = boolean variable set to False by default
- Sep = string variable containing separator character (default ¶) between array elements/file records**. Replace it by a character which is not supposed to be present in ArrayIN

OUTPUT:
- ArrayOUT = sorted array according to sorting criteria

*: assuming ArrayIN contains following elements:
#0->4
#1->2
#2->10
#3->1
In Numerical increasing order the sorted result will be:
#0->1
#1->2
#2->4
#3->10
In Alphabetical increasing order the sorted result will be:
#0->10
#1->1
#2->2
#3->4
In Alphabetical increasing order uppercase letters come before lowercase ones

**: to optimize sorting procedure, this Action block uses Script Sort command to sort file records. So, it first converts ArrayIN to "/storage/emulated/0/MacroDroid/Unsorted.txt" temporary file which is sorted according to input parameters (see above). Then it converts it into ArrayOUT

 
Last edited:

Dm114

Well-known member
What are some use case examples for your macro?
Unlike Dictionaries, sorted by key alphabetical order, Arrays items appear in the order they have been created (by index number).

Sometimes it may be interesting to display an Array content in a specific/logical order. The example given in my previous post with numerical values was mainly to show how numbers are sorted, depending on the method used.

For alphabetical values, it's much easier to understand: either Apples, Kiwis and Pears will appear in this order or reverse order...
 

Dm114

Well-known member
Simple and robust Action Block made to sort an array by 2 criteria:
- Alphabetical (default)/Numerical order*
- Increasing (default)/Decreasing order

INPUT:
- ArrayIN (compulsory) = the one-level array to be sorted
- DecreasingOrder = boolean variable set to False by default
- NumericalOrder = boolean variable set to False by default
- Sep = string variable containing separator character (default ¶) between array elements/file records**. Replace it by a character which is not supposed to be present in ArrayIN

OUTPUT:
- ArrayOUT = sorted array according to sorting criteria

*: assuming ArrayIN contains following elements:
#0->4
#1->2
#2->10
#3->1
In Numerical increasing order the sorted result will be:
#0->1
#1->2
#2->4
#3->10
In Alphabetical increasing order the sorted result will be:
#0->10
#1->1
#2->2
#3->4
In Alphabetical increasing order uppercase letters come before lowercase ones

**: to optimize sorting procedure, this Action block uses Script Sort command to sort file records. So, it first converts ArrayIN to "/storage/emulated/0/MacroDroid/Unsorted.txt" temporary file which is sorted according to input parameters (see above). Then it converts it into ArrayOUT

https://www.macrodroidlink.com/macrostore?id=17964
ℹ️ Here's a new simplified and optimized version:
- added an "ErrorMsg" return string in case of system error
- removed "Sep" separator string (became useless)

 
Top