首先按布尔列排序数组,然后按字符串列排序

时间:2017-12-21 08:23:24

标签: javascript arrays angular sorting typescript

我正在尝试按2个字段对数组进行排序。我有一个布尔值:isFavorite和一个字符串:name。 所有真正的布尔都必须是第一项。但我希望数组是字母的。到目前为止,这是我的代码(尝试了多种方法):

data.sort(function (x,y) {
    if (x.isFavorite){
        return -1;
    }
    if (x.isFavorite && !y.isFavorite && (x.name < y.name)){
        return -1;
    }  else if ((x.isFavorite === y.isFavorite) && (x.name === y.name)){
        return 0;
    } else if (x.isFavorite && y.isFavorite && (x.name < y.name)){
        return -1;
    } else if (!x.isFavorite && !y.isFavorite && (x.name > y.name)){
        return 1;
    }
}

6 个答案:

答案 0 :(得分:3)

为什么这么多条件和重叠?

只需使用logical OR运算符即可通过两个字段排序数组。

另外,我使用+ (Boolean)强制转换为数字

grouperArray.sort((a, b) => (+a.isFavorite) - (+b.isFavorite) || a.name.localeCompare(b.name));

let data = [{ isFavorite:false, name:'A' }, { isFavorite:true, name:'C' }, { isFavorite:true, name:'B' }];
data.sort((a,b) => (+b.isFavorite) - (+a.isFavorite) || a.name.localeCompare(b.name));
console.log(data);

答案 1 :(得分:2)

试试这个:

&#13;
&#13;
const data = [
  {isFavorite: false, name: 'B'}, 
  {isFavorite: false, name: 'A'}, 
  {isFavorite: true, name: 'C'}, 
  {isFavorite: true, name: 'D'},
  {isFavorite: true, name: 'A'},
  {isFavorite: false, name: 'A'},
  {isFavorite: false, name: 'D'},
  {isFavorite: true, name: 'Z'},
];

const compareLoc = (a, b) => a.name.localeCompare(b.name)
const result = [...data.filter(d => d.isFavorite).sort(compareLoc),
...data.filter(d => !d.isFavorite).sort(compareLoc)];
console.log(result);
&#13;
&#13;
&#13;

以下代码段工作。

&#13;
&#13;
const data = [
  {isFavorite: false, name: 'B'}, 
  {isFavorite: false, name: 'A'}, 
  {isFavorite: true, name: 'C'}, 
  {isFavorite: true, name: 'D'},
  {isFavorite: true, name: 'A'},
  {isFavorite: false, name: 'A'},
  {isFavorite: false, name: 'D'},
  {isFavorite: true, name: 'Z'},
];

data.sort(function (x, y) {
    // if both are true or false, we should compare name attributes
    if (x.isFavorite === y.isFavorite) {
        return x.name.localeCompare(y.name);
    } return x.isFavorite ? -1 : 1
}
)
console.log(data)
&#13;
&#13;
&#13;

答案 2 :(得分:1)

const data = [
  {isFavorite: false, name: 'b'},
  {isFavorite: false, name: 'f'},
  {isFavorite: true, name: 'c'},
  {isFavorite: true, name: 'a'},
  {isFavorite: false, name: 'd'},
  {isFavorite: true, name: 'g'}
];

const sortedData = data.sort((a, b) => {
  if (a.isFavorite !== b.isFavorite) {
    return a.isFavorite ? -1 : 1;
  } else {
    return a.name > b.name ? 1 : -1;
  }
});
  
console.log(sortedData);

答案 3 :(得分:0)

您的问题是前两个if重叠,如果ifx.isFavorite,则第二个true无法访问if {1}} return

另外,您不会检查某些情况,例如y.isFavorite truex.isFavorite不是。

你可以把它写成

if ( x.isFavorite != y.isFavorite )
{
   return x.isFavorite ? -1 : 1;
}
else
{
   return x.name.localeCompare( y.name );
}

答案 4 :(得分:0)

你走了:))

var arr = [
  {
    isFavorite: false,
    name: 'a'
  },
  {
    isFavorite: true,
    name: 'b'
  },
  {
    isFavorite: true,
    name: 'c'
  },
  {
    isFavorite: false,
    name: 'c'
  }
];

arr.sort(function (x,y) {
    if (x.isFavorite > y.isFavorite){
        return -1;
    }else if(x.isFavorite == y.isFavorite){
        return x.name > y.name;
    }else{
        return 1;
    }
});

console.log(arr);

答案 5 :(得分:-1)

如果我是你,我会创建两个数组,对它们进行排序,然后加入它们。

这看起来像这样:

let trueValues = data.filter(d => d.isFavorite);
let falseValues = data.filter(d => !d.isFavorite);

现在,排序他们!

let sortedArray = trueValues
  .sort((a, b) => a.name > b.name ? 1 : a.name < b.name ? -1 : 0)
  .concat(
    falseValues
      .sort((a, b) => a.name > b.name ? 1 : a.name < b.name ? -1 : 0)
  );