0

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 :)

3 Answers 3

2

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).

Sign up to request clarification or add additional context in comments.

4 Comments

Personally I'd go with the sort option, it's more obvious what is happening for eyeball parsing.
@JonP Actually I find the map variant easiest to grab.
sort makes it obvious we're sorting, but I'll concede map is maybe a little more obvious on the sort logic.
@connexo, thanks for your input. I’ve marked your comment as a solution based on the fact you intervened first, while the comments of the others also worked. Thank you
0

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);

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.