I have this unsorted array (containing objects)
toSortArray = [{taskID: 1, "title": "something1", subtasks: {}},
{taskID: 5, "title": "something5", subtasks: {}},
{taskID: 8, "title": "something8", subtasks: {}}];
and I have this array that is dynamically populated based on the correct positioning of the taskIDs
sortingArray = [8, 1, 5];
What I am trying to do is to sort 'toSortArray' with the exact same order as stated in 'sortingArray'.
I have found a somewhat called solution here stating this:
var arr = ['one','four','two'];
var test = [{
key: 'one'
},{
key: 'two'
},{
key: 'four'
}];
function sortFunction(a,b){
var indexA = arr.indexOf(a['key']);
var indexB = arr.indexOf(b['key']);
if(indexA < indexB) {
return -1;
}else if(indexA > indexB) {
return 1;
}else{
return 0;
}
}
However, it doesn't work for me. I replaced the placeholders for:
function sortFunction (a,b) {
var indexA = sortingArray.indexOf(a["taskID"]);
console.log(indexA);
var indexB = sortingArray.indexOf(b["taskID"]);
console.log(indexB);
if (indexA < indexB) {
return -1;
} else if (indexA > indexB) {
return 1;
} else {
return 0;
}
}
When I debugged (as you can see I am console logging the indexA and indexB). They always return -1, which means no such index is found.
My question is how to get to compare "taskID" value and the sortingArray elements.
Thank you :)
CodePudding user response:
Disclaimer: In the examples, I've remove properties not relevant to the question, and also renamed taskID to tID for brevity.
You can use Array.prototype.reduce to achieve the desired result:
let arr = [{tID: 1},{tID: 5},{tID: 8}];
let sArr = [8, 1, 5];
const result = sArr.reduce((a,v) => a.concat(arr.find(({tID})=>tID===v)), []);
console.log(result);
You can also use Array.prototype.sort:
let arr = [{tID: 1},{tID: 5},{tID: 8}];
let sArr = [8, 1, 5];
const result = arr.sort(
({tID: aID}, {tID: bID}) => sArr.indexOf(aID) - sArr.indexOf(bID));
console.log(result);
Array.prototype.map will also do the job:
let arr = [{tID: 1},{tID: 5},{tID: 8}];
let sArr = [8, 1, 5];
const result = sArr.map(i => arr.find(({tID}) => tID === i));
console.log(result);
map and reduce will probably perform faster than sort because each iteration only requires a single lookup (find) in the toSortArray, as opposed to sort, which requires two lookups (indexOf).
CodePudding user response:
You can filter the toSortArray based upon each of the sortingArray element values. Using filter will return an array of one element that matches the current value. Since filter returns an array, use flatMap() to iterate over the sortingArray and return a single, sorted array in the order of the sortingArray.
const toSortArray = [{taskID: 1, "title": "something1", subtasks: {}},
{taskID: 5, "title": "something5", subtasks: {}},
{taskID: 8, "title": "something8", subtasks: {}}];
const sortingArray = [8, 1, 5];
const result = sortingArray.flatMap(so=>toSortArray.filter(ts=>ts.taskID === so));
console.log(result);
CodePudding user response:
You can use map and find as:
toSortArray = [{ taskID: 1, "title": "something1", subtasks: {} },
{ taskID: 5, "title": "something5", subtasks: {} },
{ taskID: 8, "title": "something8", subtasks: {} }];
sortingArray = [8, 1, 5];
const result = sortingArray.map((item) => toSortArray.find((element) => element.taskID === item));
console.log(result);
