Reputation: 33
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());
});
Upvotes: 3
Views: 53
Reputation: 1250
Here is an example where currying is being used to first sort the array and then swap two objects by providing two keywords
if both of them exist in the sorted array.
const a = [
{keyword: 'E', value: '5'},
{keyword: 'C', value: '3'},
{keyword: 'B', value: '2'},
{keyword: 'D', value: '4'},
{keyword: 'A', value: '1'},
{keyword: 'F', value: '6'},
]
//Using currying where the function is broken down into a series of functions
const sortSwap = (arrayToSort) => {
//first sort it
const sortedArray = arrayToSort.sort((a,b) => a.keyword.toLowerCase() > b.keyword.toLowerCase());
return (swap1, swap2) => {
//now swap it
const item1 = sortedArray.findIndex(elem => elem.keyword === swap1);
const item2 = sortedArray.findIndex(elem => elem.keyword === swap2);
//if actually both keywords are existing in the array, proceed to swap
if (item1 >= 0 && item2 >= 0) {
const temp = sortedArray[item1];
sortedArray[item1] = sortedArray[item2];
sortedArray[item2] = temp;
}
return sortedArray;
}
}
//call the function by using currying
console.log(sortSwap(a)('A', 'C'));
Upvotes: 0
Reputation: 5522
var data = [
{keyword: 'E', value: '5'},
{keyword: 'C', value: '3'},
{keyword: 'B', value: '2'},
{keyword: 'D', value: '4'},
{keyword: 'A', value: '1'},
{keyword: 'F', value: '6'},
]
var swapData =['C','D'];
data = data.sort((a,b)=> a.keyword < b.keyword?-1:1);
var [first,second] = [data.findIndex((val)=>val.keyword == swapData[0]),data.findIndex((val)=>val.keyword == swapData[1])];
var temp = data[first];
data[first] = data[second];
data[second] = temp;
console.log(data);
Upvotes: 0
Reputation: 24965
var myArray = [
{keyword: 'E', value: '5'},
{keyword: 'C', value: '3'},
{keyword: 'B', value: '2'},
{keyword: 'D', value: '4'},
{keyword: 'A', value: '1'},
{keyword: 'F', value: '6'}
];
var specialKeywords = [ 'C', 'D' ];
myArray.sort(function(a, b){
//if the two being compared are 'C' and 'D', treat them special
if (specialKeywords.indexOf(a.keyword) +1
&& specialKeywords.indexOf(b.keyword) +1) {
//if a is 'C', it needs to be greater than 'D'
if (a.keyword = 'C') return 1;
else return -1;
} else {
//one of the elements is not 'C' or 'D', process normally.
return a.keyword.toLowerCase().localeCompare(b.keyword.toLowerCase());
}
});
console.log(myArray);
Upvotes: 2