如何使用自定义顺序对对象数组进行排序?

时间:2018-05-31 19:59:32

标签: javascript jquery

我有一个像这样的对象数组:

[
    {keyword: 'E', value: '5'},
    {keyword: 'C', value: '3'},
    {keyword: 'B', value: '2'},
    {keyword: 'D', value: '4'},
    {keyword: 'A', value: '1'},
    {keyword: 'F', value: '6'},
    ...
]

我从其他地方收到这个数组,我无法控制源,它的顺序也可以完全随机。

现在我想在关键字上按升序对数组进行排序,但交换2个对象除外。我知道要交换的对象的关键字,比方说C和D在上面的数组中。所以最后的结果我希望它是这样的:

[
    {keyword: 'A', value: '1'},
    {keyword: 'B', value: '2'},
    {keyword: 'D', value: '4'},
    {keyword: 'C', value: '3'},
    {keyword: 'E', value: '5'},
    {keyword: 'F', value: '6'},
    ...
]

我必须遵循代码,但我不知道在哪里放置其余的代码。请帮忙!

myArray.sort(function(a, b){
     return a.keyword.toLowerCase().localeCompare(b.keyword.toLowerCase());
});

3 个答案:

答案 0 :(得分:2)

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);

答案 1 :(得分:0)



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);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

下面是一个示例,其中currying用于首先对数组进行排序,然后通过提供两个keywords来交换两个对象(如果它们都存在于已排序的数组中)。

&#13;
&#13;
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'));
&#13;
&#13;
&#13;