Skip to content
Snippets Groups Projects
Commit ae773a83 authored by S.Listl's avatar S.Listl
Browse files

Tree sort function

parent 9fe74584
No related branches found
No related tags found
No related merge requests found
......@@ -81,7 +81,7 @@ var attributeValues = db.table(attributeSql).map(function (row)
if (getTree)
_fetchAttributes(attributeValues.map(function (row) {return row[1]}));
allAttributes = _sortArrayForTree(allAttributes).concat(attributeValues);
allAttributes = TreeUtils.sortArrayForTree(allAttributes).concat(attributeValues);
result.object(allAttributes);
......
......@@ -540,3 +540,44 @@ NumberSequencingUtils.getMaxUniqueNumber = function(pColumn, pTable, pCondition)
var maxNum = db.cell("select max(" + pColumn + ") from " + pTable + condition);
return maxNum == "" ? "0" : maxNum;
}
/**
* functions for trees
*/
function TreeUtils () {}
/**
* sorts an array in a way that a tree(-table) can be built
*
* @param {Array} pArray two-dimensional array to sort
* @param {Number} pUidIndex the index of the uid in a row
* @param {Number} pParentIdIndex the index of the parent id in a row
*
* @return {Array} the sorted array
*/
TreeUtils.sortArrayForTree = function (pArray, pUidIndex, pParentIdIndex)
{
if (pArray.length <= 1)
return pArray;
var rows = {};
var allIds = {};
pArray.forEach(function (row) {allIds[row[pUidIndex]] = true;});
var index = 0;
do {
var oldIndex = index;
pArray.forEach(function (row)
{
if (!(row[pUidIndex] in this) && (row[pParentIdIndex] in this || !allIds[row[pParentIdIndex]]))
this[row[pUidIndex]] = {
data : row,
index : index++
};
}, rows);
} while (oldIndex != index);
var sortedArray = new Array(index);
for (let i in rows)
sortedArray[rows[i].index] = rows[i].data;
return sortedArray;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment