I have an object array like this:
[
{keyword: 'E', value: '5'},
{keyword: 'C', value: '3'},
{keyword: 'B', value: '2'},
{keyword: 'D', value: '4'},
{keyword: 'A', value: '1'},
{keyword: 'F', value: '6'},
...
]
I receive this array from other places and I have no control over the source, also the order that it comes can be completely random.
Now I want to sort the array with ascending order on keyword, with the exception of swapping 2 objects. I know the keyword of the object that I want to swap, let say C, and D in the above array. So the final result I want it to be like this:
[
{keyword: 'A', value: '1'},
{keyword: 'B', value: '2'},
{keyword: 'D', value: '4'},
{keyword: 'C', value: '3'},
{keyword: 'E', value: '5'},
{keyword: 'F', value: '6'},
...
]
I have to following code, but I don't know where to put the rest of the code. Please help!
myArray.sort(function(a, b){
return a.keyword.toLowerCase().localeCompare(b.keyword.toLowerCase());
});