You you can install via yarn add locutus and
require this function via const array_multisort = require('locutus/php/array/array_multisort').
It is important to use a bundler that supports tree-shaking
so that you only ship the functions that you actually use to your browser,
instead of all of Locutus, which is massive. Examples are:
Parcel,
webpack, or
rollup.js.
For server-side use this is typically less of a concern.
Examples
Please note that these examples are distilled from test cases that automatically verify
our functions still work correctly. This could explain some quirky ones.
Please note that Locutus uses JavaScript objects as substitutes for PHP arrays,
they are the closest we can get to this hashtable-like data structure without
rolling our own. While many JavaScript implementations preserve the order of object properties, the
ECMAScript Language Specification
explicitly states that:
The mechanics and order of enumerating the properties is not specified.
So don't use this for anything serious if you rely on the order to be consistent accross platforms.
To influence how Locutus treats objects to arrays, you can check out the `locutus.objectsAsArrays` setting.
Notes
flags: Translation table for sort arguments.
Each argument turns on certain bits in the flag byte through addition.
bits: HGFE DCBA
args: Holds pointer to arguments for reassignment
const sortDuplicator = function (a, b) { return nLastSort.shift() }
const sortFunctions = [ [ function (a, b) { lastSort.push(a > b ? 1 : a < b ? -1 : 0) return a > b ? 1 : a < b ? -1 : 0 }, function (a, b) { lastSort.push(b > a ? 1 : b < a ? -1 : 0) return b > a ? 1 : b < a ? -1 : 0 }, ], [ function (a, b) { lastSort.push(a - b) return a - b }, function (a, b) { lastSort.push(b - a) return b - a }, ], [ function (a, b) { lastSort.push(a + '' > b + '' ? 1 : a + '' < b + '' ? -1 : 0) return a + '' > b + '' ? 1 : a + '' < b + '' ? -1 : 0 }, function (a, b) { lastSort.push(b + '' > a + '' ? 1 : b + '' < a + '' ? -1 : 0) return b + '' > a + '' ? 1 : b + '' < a + '' ? -1 : 0 }, ], ]
const sortArrs = [[]]
const sortKeys = [[]]
// Store first argument into sortArrs and sortKeys if an Object. // First Argument should be either a Javascript Array or an Object, // otherwise function would return FALSE like in PHP if (Object.prototype.toString.call(arr) === '[object Array]') { sortArrs[0] = arr } elseif (arr && typeof arr === 'object') { for (i in arr) { if (arr.hasOwnProperty(i)) { sortKeys[0].push(i) sortArrs[0].push(arr[i]) } } } else { returnfalse }
// arrMainLength: Holds the length of the first array. // All other arrays must be of equal length, otherwise function would return FALSE like in PHP // sortComponents: Holds 2 indexes per every section of the array // that can be sorted. As this is the start, the whole array can be sorted. const arrMainLength = sortArrs[0].length let sortComponents = [0, arrMainLength]
// Loop through all other arguments, checking lengths and sort flags // of arrays and adding them to the above variables. const argl = arguments.length for (j = 1; j < argl; j++) { if (Object.prototype.toString.call(arguments[j]) === '[object Array]') { sortArrs[j] = arguments[j] sortFlag[j] = 0 if (arguments[j].length !== arrMainLength) { returnfalse } } elseif (arguments[j] && typeofarguments[j] === 'object') { sortKeys[j] = [] sortArrs[j] = [] sortFlag[j] = 0 for (i inarguments[j]) { if (arguments[j].hasOwnProperty(i)) { sortKeys[j].push(i) sortArrs[j].push(arguments[j][i]) } } if (sortArrs[j].length !== arrMainLength) { returnfalse } } elseif (typeofarguments[j] === 'string') { const lFlag = sortFlag.pop() // Keep extra parentheses around latter flags check // to avoid minimization leading to CDATA closer if (typeof flags[arguments[j]] === 'undefined' || ((flags[arguments[j]] >>> 4) & (lFlag >>> 4)) > 0) { returnfalse } sortFlag.push(lFlag + flags[arguments[j]]) } else { returnfalse } }
for (i = 0; i !== arrMainLength; i++) { thingsToSort.push(true) }
// Sort all the arrays.... for (i in sortArrs) { if (sortArrs.hasOwnProperty(i)) { lastSorts = [] tmpArray = [] elIndex = 0 nLastSort = [] lastSort = []
// If there are no sortComponents, then no more sorting is neeeded. // Copy the array back to the argument. if (sortComponents.length === 0) { if (Object.prototype.toString.call(arguments[i]) === '[object Array]') { args[i] = sortArrs[i] } else { for (k inarguments[i]) { if (arguments[i].hasOwnProperty(k)) { deletearguments[i][k] } } sal = sortArrs[i].length for (j = 0, vkey = 0; j < sal; j++) { vkey = sortKeys[i][j] args[i][vkey] = sortArrs[i][j] } } sortArrs.splice(i, 1) sortKeys.splice(i, 1) continue }
// Sort function for sorting. Either sorts asc or desc, regular/string or numeric. let sFunction = sortFunctions[sortFlag[i] & 3][(sortFlag[i] & 8) > 0 ? 1 : 0]
// Sort current array. for (l = 0; l !== sortComponents.length; l += 2) { tmpArray = sortArrs[i].slice(sortComponents[l], sortComponents[l + 1] + 1) tmpArray.sort(sFunction) // Is there a better way to copy an array in Javascript? lastSorts[l] = [].concat(lastSort) elIndex = sortComponents[l] for (g in tmpArray) { if (tmpArray.hasOwnProperty(g)) { sortArrs[i][elIndex] = tmpArray[g] elIndex++ } } }
// Duplicate the sorting of the current array on future arrays. sFunction = sortDuplicator for (j in sortArrs) { if (sortArrs.hasOwnProperty(j)) { if (sortArrs[j] === sortArrs[i]) { continue } for (l = 0; l !== sortComponents.length; l += 2) { tmpArray = sortArrs[j].slice(sortComponents[l], sortComponents[l + 1] + 1) // alert(l + ':' + nLastSort); nLastSort = [].concat(lastSorts[l]) tmpArray.sort(sFunction) elIndex = sortComponents[l] for (g in tmpArray) { if (tmpArray.hasOwnProperty(g)) { sortArrs[j][elIndex] = tmpArray[g] elIndex++ } } } } }
// Duplicate the sorting of the current array on array keys for (j in sortKeys) { if (sortKeys.hasOwnProperty(j)) { for (l = 0; l !== sortComponents.length; l += 2) { tmpArray = sortKeys[j].slice(sortComponents[l], sortComponents[l + 1] + 1) nLastSort = [].concat(lastSorts[l]) tmpArray.sort(sFunction) elIndex = sortComponents[l] for (g in tmpArray) { if (tmpArray.hasOwnProperty(g)) { sortKeys[j][elIndex] = tmpArray[g] elIndex++ } } } } }
// Generate the next sortComponents zlast = null sortComponents = [] for (j in sortArrs[i]) { if (sortArrs[i].hasOwnProperty(j)) { if (!thingsToSort[j]) { if (sortComponents.length & 1) { sortComponents.push(j - 1) } zlast = null continue } if (!(sortComponents.length & 1)) { if (zlast !== null) { if (sortArrs[i][j] === zlast) { sortComponents.push(j - 1) } else { thingsToSort[j] = false } } zlast = sortArrs[i][j] } else { if (sortArrs[i][j] !== zlast) { sortComponents.push(j - 1) zlast = sortArrs[i][j] } } } }
if (sortComponents.length & 1) { sortComponents.push(j) } if (Object.prototype.toString.call(arguments[i]) === '[object Array]') { args[i] = sortArrs[i] } else { for (j inarguments[i]) { if (arguments[i].hasOwnProperty(j)) { deletearguments[i][j] } }
Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows
The McDonald’s Theory.
This means that we assimilate first iterations with imperfections,
hoping for others to take issue with-and improve them.
This unorthodox approach has worked very well to foster fun and fruitful collaboration,
but please be reminded to use our creations at your own risk.
THE SOFTWARE IS PROVIDED "AS IS" has never been more true than for Locutus.