-3

Given are these exemplary arrays:

var questions = [
  {
    question: 'Which of these U.S. Presidents appeared on the television series "Laugh-In"?',
    hint1:    '...',
    hint2:    '...',
    hint3:    '...'
  },
  {
    question: 'The Earth is approximately how many miles away from the Sun?',
    hint1:    '...',
    hint2:    '...',
    hint3:    '...'
  },
  {
    question: 'Which insect shorted out an early supercomputer and inspired the term "computer bug"?',
    hint1:    '...',
    hint2:    '...',
    hint3:    '...'
  }
];

var answersToFirstQuestion = [
  {
    answer:  'Lyndon Johnson',
    comment: '...'
  },
  {
    answer:  'Richard Nixon',
    comment: '...'
  },
  {
    answer:  'Jimmy Carter',
    comment: '...'
  },
  {
    answer:  'Gerald Ford',
    comment: '...'
  }
];

var anwersToSecondQuestion = [
  {
    answerA: '9.3 million',
    comment: '...'
  },
  {
    answerB: '39 million',
    comment: '...'
  },
  {
    answerC: '93 million',
    comment: '...'
  },
  {
    answerD: '193 million',
    comment: '...'
  }
];

var answersToThirdQuestion = [
  {
    answerA: 'Moth',
    comment: '...'
  },
  {
    answerB: 'Roach',
    comment: '...'
  },
  {
    answerC: 'Fly',
    comment: '...'
  },
  {
    answerD: 'Japanese beetle',
    comment: '...'
  }
];

Notwithstanding the fact that it may not make sense to store the answers this way, it is just an example, how can the answer-arrays be sorted according to an order given in the form of a string?

So something like console.log(anwersToSecondQuestion.sort('A, C, B, D')); should result in:

[
  {
    "answer": "Lyndon Johnson",
    "comment": "..."
  },
  {
    "answer": "Jimmy Carter",
    "comment": "..."
  },
  {
    "answer": "Richard Nixon",
    "comment": "..."
  },
  {
    "answer": "Gerald Ford",
    "comment": "..."
  }
]
Ben
  • 1,550
  • 1
  • 16
  • 24
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – ponury-kostek Oct 12 '21 at 17:36
  • Not really. As I don't want to sort by property value. – Ben Oct 12 '21 at 17:37
  • Please visit [help], take [tour] to see what and [ask]. Do some research, ***[search for related topics on SO](https://www.google.com/search?q=javascript+sort+array+based+on+list+site:stackoverflow.com)***; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Oct 12 '21 at 17:46

1 Answers1

0

Can be done with a very custom sort function.

Note : Sort does not return anything

var anwersToSecondQuestion = [
  {
    answerA: '9.3 million',
    comment: '...'
  },
  {
    answerB: '39 million',
    comment: '...'
  },
  {
    answerC: '93 million',
    comment: '...'
  },
  {
    answerD: '193 million',
    comment: '...'
  }
];


Array.prototype.veryCustomSort = function (sortOrder)  {
this.sort((a,b) => {
let keyTypeA = Object.keys(a).filter(x => x.indexOf('answer') > -1)[0];
let [,characterA] = keyTypeA.split('answer');
let keyTypeB = Object.keys(b).filter(x => x.indexOf('answer') > -1)[0];

let [,characterB] = keyTypeB.split('answer');

if(sortOrder.indexOf(characterA) > sortOrder.indexOf(characterB)){
return 1;
}
return -1;
})
}

anwersToSecondQuestion.veryCustomSort('A, C, B, D');
console.log(anwersToSecondQuestion);
Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39