Search code examples
sortingautoit

User-defined sorting comparison using AutoIt


PHP has usort and Java has the Comparator interface.

These allow you to create a simple function that compare two Objects, Strings, etc. without getting involved in the details of the sort implementation.


Generally, the comparison function looks something like this:

// Return value:
//    Negative value: a before b
//    Positive value: b before a
//    Zero:           strings are equal
function compare(String a, String b) {
    return toLowerCase(a) - toLowerCase(b);
}

... and you can be as simple or as fancy as you like with your comparisons.


Is there anything in AutoIt that does this?

The documentation is great, and the Help File is great, but I cannot find anything that allows me to define a custom comparison function. (I will reimplement Quicksort if necessary, but with a framework as full-featured as AutoIt, I feel like I must just be overlooking something.)


Solution

  • "Is there anything in AutoIt that does this?"

    Example as per _ArrayMultiColSort() :

    #include <Array.au3>
    #include "ArrayMultiColSort.au3"
    
    Global Const $g_sHeaderRow = 'category|value'
    Global Const $g_aSortOrder = [ _
                                    [0, 'critical,important,regular'], _
                                    [1, 1] _
                                 ]
    Global       $g_aArray     = [ _
                                    ['critical',  7], _
                                    ['important', 2], _
                                    ['important', 6], _
                                    ['regular',   2], _
                                    ['critical',  5], _
                                    ['regular',   9] _
                                 ]
    
    _ArrayMultiColSort($g_aArray, $g_aSortOrder)
    _ArrayDisplay($g_aArray, @ScriptName, '', 0, Default, $g_sHeaderRow)
    

    Returns:

    critical  7
    critical  5
    important 6
    important 2
    regular   9
    regular   2