Special Sorting

Indexing a collection of objects based on a variable

Make a copy of the information held in the controller variable or function in the original collection of objects and feed it to a {v}ector.

var vMaster = []

for (var i in oAlias.Cells) {
    vMaster.push(oAlias.Cells[i].Lookup_length())
}

Create a sorted {v}ector based on the controller variable. Reverse the results as necessary.

var vSorted = Bubble_sort(vMaster)

vSorted.reverse()

Create 2 {b}inary and 1 {s}tring variable to check (1) whether a particular position in the sorted {v}ector is still available or BLOCKED, and (2) if the controller variable and the sorted {v}ector are identical.

A further, temporary controller variable is created to enhance performance (preventing unnecessary re-iterations of the same object-based function over and over again in the time-consuming inner loop).

var bBlocked = false
var bIdentical = false
var sBlocked = "BLOCKED"
var nMaster

Create 2 nested loops based on the collection of objects (the dimensions of the analyses stay the same in each new step).

for (var i in oAlias.Cells) {
    nMaster = oAlias.Cells[i].Lookup_length()

    for (var j in oAlias.Cells) {

First, test the availability of the sorted variable given that this is a primary prerequisite…

        bBlocked = (vSorted[j] === sBlocked)

        if (!bBlocked) {

…if available, test for equality within the sorted variable and the controller variable of the original collection of objects.

            bIdentical = (vSorted[j] === nMaster)

            if (bIdentical) {

If identical, feed the matching position's ID of the inner loop back to the dependent variable of the outer loop. It is possible to rely on the iterator ‘j’, but this is an error-prone strategy given that ‘j’ is zero-based.

                oAlias.Cells[i].Dependent.Value = ⤶
                 oAlias.Cells[j].ID.Value

Remember (1) to block the position of the sorted variable (to ascertain the unavailability of that particular position in future iterations), and (2) to break the execution of the inner loop (to ascertain that only one position of the sorted variable is being blocked with each iteration of the outer loop).

                vSorted[j] = sBlocked
                break
            }
        }
    }
}

CODE ONLY

var vMaster = []

for (var i in oAlias.Cells) {
    vMaster.push(oAlias.Cells[i].Lookup_length())
}

var vSorted = Bubble_sort(vMaster)

vSorted.reverse()

var bBlocked = false
var bIdentical = false
var sBlocked = "BLOCKED"
var nMaster

for (var i in oAlias.Cells) {
    nMaster = oAlias.Cells[i].Lookup_length()

    for (var j in oAlias.Cells) {
        bBlocked = (vSorted[j] === sBlocked)

        if (!bBlocked) {
            bIdentical = (vSorted[j] === nMaster)

            if (bIdentical) {
                oAlias.Cells[i].Dependent.Value = ⤶
                 oAlias.Cells[j].ID.Value

                vSorted[j] = sBlocked
                break
            }
        }
    }
}

Presenting the Collection in Numerical Order

Create a {v}ector which converts the sequence of dependent positions into an order of indices that can be iterated. In this step, it is necessary to decrement the value of the dependent positions by 1, given that the iteration has to be zero-based.

Note that the decrement has to be in calculated form (‘x - 1’ rather than ‘x--’), or the value of the ‘slave’ position itself will be altered.

var vIndices = []

for (var i in oAlias.Cells) {
    vIndices[oAlias.Cells[i].Dependent.Value - 1] = i
}

Create a {v}ector or {s}tring that sums up the positioned information in the desired order by nesting the indices.

var sResult = ""

for (var i in oAlias.Cells) {
    sResult += oAlias.Cells[vIndices[i]].Dump()
}

CODE ONLY

var vIndices = []

for (var i in oAlias.Cells) {
    vIndices[oAlias.Cells[i].Dependent.Value - 1] = i
}

var sResult = ""

for (var i in oAlias.Cells) {
    sResult += oAlias.Cells[vIndices[i]].Dump()
}