24 lines
1.1 KiB
TypeScript
24 lines
1.1 KiB
TypeScript
export function shuffleArray(array : any[]) {
|
|
for (var i = array.length - 1; i > 0; i--) {
|
|
var j = Math.floor(Math.random() * (i + 1));
|
|
var temp = array[i];
|
|
array[i] = array[j];
|
|
array[j] = temp;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sorts all arrays together with the first. Pass either a list of arrays, or a map. Any key is accepted.
|
|
* Array|Object arrays [sortableArray, ...otherArrays]; {sortableArray: [], secondaryArray: [], ...}
|
|
* Function comparator(?,?) -> int optional compareFunction, compatible with Array.sort(compareFunction)
|
|
*/
|
|
export function sortArrays(arrays : any[][], comparator = (a:any, b:any) => (a < b) ? 1 : (a > b) ? -1 : 0) {
|
|
let arrayKeys = Object.keys(arrays);
|
|
let sortableArray : (typeof arrays[0][0]) = Object.values(arrays)[0];
|
|
let indexes = Object.keys(sortableArray);
|
|
let sortedIndexes = indexes.sort((a, b) => comparator(sortableArray[a], sortableArray[b]));
|
|
|
|
let sortByIndexes = (array : any[], sortedIndexes : number[]) => sortedIndexes.map(sortedIndex => array[sortedIndex]);
|
|
|
|
return arrayKeys.map(arrayIndex => sortByIndexes(arrays[arrayIndex], sortedIndexes));
|
|
} |