JavaScript如何按照布尔属性对对象数组进行排序

164

请看结尾处的编辑以了解实际问题。

好的,我有一个场景:

a = [false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]

那么如果我这样做:

a.sort(function(a,b){return !a && b});

它给了我这个:

[false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]

有点像排序...但又不完全一样... :(

我该怎么对这个数组进行排序?

编辑:

如果你想知道为什么我没有使用a.sort(),是因为我的实际数组是对象数组,而不是像我发帖时那样的普通数组。实际数组的元素看起来像 [{xx:true},{xx:false},...]


如果我执行a.map(function(x){ return x?1:0 }).sort(function(a,b){return a>b});同样不起作用... 我觉得我可能做了一些基本错误的事情 - PCoelho
为什么需要编写自定义函数?a.sort() 应该可以工作。 - Haseeb Asif
1
有没有一种使用lodash的方法? - Mina Fawzy
15个回答

1
a=[true,false,true,false,true];
 
a.sort(function(x, y) {
      a1=x?1:0
      b1=y?1:0
return a1-b1
    });

1

布尔数组需要有足够的条目来表示所有的转换,即从true到true,从true到false,从false到false,从false到true。

var boolarray = [true, false, true, true, false, false, true]
boolarray.sort( (a,b) => !(a ^ b) ? 0 : a ? -1 : 1)

这个排序方法是对输入的异或值进行反转。如果输入相同,则返回0;如果不同,则如果“a”为true,“b”必须为false,因此返回-1,反之返回1。

当'a'和'b'不同时,它们被排序,当它们相同时则被忽略。

要将此方法用于对象,请使用对象成员名称作为排序参数:

var objarray = [{xx:true}, {xx:false}, {xx:true}, {xx:true}, {xx:false}, {xx:false}, {xx:true}]
objarray.sort( (a,b) => !(a.xx ^ b.xx) ? 0 : a.xx ? -1 : 1)

“^” 操作符不允许用于布尔类型。请考虑使用 “!==” 替代。 - Ste

0

我在 return (x.xx === y.xx) ? 0 : x ? -1 : 1; 上遇到了 TypeScript 错误。

当您想要按布尔属性排序时,这是我的解决方案。

this.mediaList.sort( (a: MediaAutosubscriptionModel, b: MediaAutosubscriptionModel) => {
    let status1: number = a.status === StatusEnum.ACTIVE ? 1 : 0;
    let status2: number = b.status === StatusEnum.ACTIVE ? 1 : 0;
    let comparison: number = 0;
    let direction: number = this.sortDirection === SortDirectionsEnum.ASC ? -1 : 1;
    if (status1 > status2) {
        comparison = direction;
    } else if (status1 < status2) {
        comparison = -1 * direction;
    }
        return comparison;
    });

0

不使用任何ES6函数,具有时间和空间优化 -

const data = [false, true, true, true, false, false, false, true];
let lastElementUnchecked;
for(let i=0; i<data.length; i++){
    if(data[i] && lastElementUnchecked !== undefined){
        let temp = data[i];
        data[i] = data[lastElementUnchecked];
        data[lastElementUnchecked] = temp;
        i = lastElementUnchecked;
        lastElementUnchecked = undefined;
    }else{
        if(!data[i] && lastElementUnchecked === undefined){
            lastElementUnchecked = i;
        }
    }
}
console.log(data)


0
const arr = [{ bool: true }, { bool: false }, { bool: false }, { bool: true }];

// Sorting the array to place true values first
const trueValFirst = arr.sort((a, b) => {
  // Converting boolean values to numbers and comparing them
  const boolA = Number(a.bool);
  const boolB = Number(b.bool);
  
  // Sorting in descending order
  return boolB - boolA;
});

console.log(trueValFirst);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接