0

Given array:

var someAnswers = [
  {
    answer:  'Lyndon Johnson', // answer A
    comment: '...'
  },
  {
    answer:  'Richard Nixon', // answer B
    comment: '...'
  },
  {
    answer:  'Jimmy Carter', // answer C
    comment: '...'
  },
  {
    answer:  'Gerald Ford', // answer D
    comment: '...'
  }
];

Some custom order:

customOrder = 'A, C, B, D';

or

customOrder = ['A', 'C', 'B', 'D'];

Do something like this:

someAnswers.sort(customOrder);

Desired result:

[
  {
    "answer": "Lyndon Johnson",
    "comment": "..."
  },
  {
    "answer": "Jimmy Carter",
    "comment": "..."
  },
  {
    "answer": "Richard Nixon",
    "comment": "..."
  },
  {
    "answer": "Gerald Ford",
    "comment": "..."
  }
]

Another custom order:

anotherCustomOrder = 'D, B, A, C';

or

anotherCustomOrder = ['D', 'B', 'A', 'C'];

Do something like this:

someAnswers.sort(anotherCustomOrder);

Desired result:

[
  {
    "answer": "Gerald Ford",
    "comment": "..."
  },
  {
    "answer": "Richard Nixon",
    "comment": "..."
  },
  {
    "answer": "Lyndon Johnson",
    "comment": "..."
  },
  {
    "answer": "Jimmy Carter",
    "comment": "..."
  }
]
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Ben
  • 1,550
  • 1
  • 16
  • 24
  • How are "Gerald Ford" and 'D' associated? – Heretic Monkey Oct 12 '21 at 20:13
  • This is most likely a duplicate of [How do I sort an array of objects based on the ordering of another array?](https://stackoverflow.com/questions/9755889/how-do-i-sort-an-array-of-objects-based-on-the-ordering-of-another-array) – Heretic Monkey Oct 12 '21 at 20:17
  • Just because "Gerald Ford" is the fourth answer (and "D" is the fourth letter in the alphabet). – Ben Oct 12 '21 at 20:25
  • Does this answer your question? [How do I sort an array of objects based on the ordering of another array?](https://stackoverflow.com/questions/9755889/how-do-i-sort-an-array-of-objects-based-on-the-ordering-of-another-array) – Heretic Monkey Oct 12 '21 at 20:26
  • Not really. Because there are IDs which I don't have. – Ben Oct 12 '21 at 20:29

3 Answers3

1

    const sorting = (currentArray, indexArr) => {
        const reArrangedArr = [];
        const deepCopied = JSON.parse(JSON.stringify(currentArray));
        indexArr.forEach(index => reArrangedArr.push(deepCopied[index]));
        return reArrangedArr;
    }
    var someAnswers = [
      {
        answer:  'Lyndon Johnson', // answer A
        comment: '...'
      },
      {
        answer:  'Richard Nixon', // answer B
        comment: '...'
      },
      {
        answer:  'Jimmy Carter', // answer C
        comment: '...'
      },
      {
        answer:  'Gerald Ford', // answer D
        comment: '...'
      }
    ];
    console.log(sorting(someAnswers, [3,0,1,2]))
    
Paul
  • 296
  • 1
  • 4
  • 16
1

If you were willing to replace the letters with numbers in customOrder, you could do something like this:

customOrder = [0, 2, 1, 3];

sort(someAnswers, customOrder) {
    res = [];
    customOrder.forEach((n) => {
        res.push(someAnswers[n]);
    }
    return res;
}

Alternatively, if you really want to use letters:

customOrder = ["A", "C", "B", "D"];

sort(someAnswers, customOrder) {
    res = [];
    customOrder.forEach((n) => {
        res.push(someAnswers[n.charCodeAt(0) - 65]);
    }
    return res;
}
1

You can create an object with the indexes according to the desired order, and then use the function Array.prototype.map and extract the values by using the previously created indexes array.

const someAnswers = [  {    answer:  'Lyndon Johnson', comment: '...'  },  {    answer:  'Richard Nixon',    comment: '...'  },  {    answer:  'Jimmy Carter',     comment: '...'  },  {    answer:  'Gerald Ford',    comment: '...'  }],
      answerIndexes = ['A', 'B', 'C', 'D'].reduce((a, c, i) => ({...a, [c]: i}), {}),
      customOrder = ['A', 'C', 'B', 'D'],
      sorted = customOrder.map(L => someAnswers[answerIndexes[L]]);

console.log(sorted);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Ele
  • 33,468
  • 7
  • 37
  • 75
  • Thank you! Could you explain the part where the magic happens (`reduce((a, c, i) => ({...a, [c]: i}), {})`) and the `L` in `map()`? – Ben Oct 15 '21 at 20:36