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": "..."
}
]