在对象数组中排序空值

时间:2022-09-25 12:40:36

I have an array of objects as follows:

我有一个对象数组,如下所示:

c = [{a: null}, {a: 12}, {a: 1}, {a: 50}, {a: 2}, {a: null}]

I want to kind-of sort them by the objects having values first then objects with null.

我想把它们排序,首先是具有值的对象,然后是具有null的对象。

What I tried is:

我尝试的是:

c.sort(function(b) { return b.a ? -1 : 1 })

OUTPUT

输出

[{a: 2}, {a: 50}, {a: 1}, {a: 12}, {a: null}, {a: null}]

EXPECTED OUTPUT

预期的输出

[{a: 12}, {a: 1}, {a: 50}, {a: 2}, {a: null}, {a: null}]

How can I achieve this?

我如何做到这一点?

6 个解决方案

#1


2  

You could test the value. If null, then take the delta of the comparison.

你可以测试这个值。如果为零,则取比较的增量。

var c = [{ a: null }, { a: 12 }, { a: 1 }, { a: 50 }, { a: 2 }, { a: null }];

c.sort(function (a, b) {
    return (a.a === null) - (b.a === null);
});

console.log(c);
.as-console-wrapper { max-height: 100% !important; top: 0; }

For a stable sort, you could use sorting with map and use the indices as second sort option.

对于稳定排序,可以使用与map的排序并使用索引作为第二种排序选项。

// the array to be sorted
var list = [{ a: null }, { a: 12 }, { a: 1 }, { a: 50 }, { a: 2 }, { a: null }];

// temporary array holds objects with position and sort-value
var mapped = list.map(function(el, i) {
    return { index: i, value: el.a === null};
});

// sorting the mapped array containing the reduced values
mapped.sort(function(a, b) {
    return a.value - b.value || a.index - b.index;
});

// container for the resulting order
var result = mapped.map(function(el){
    return list[el.index];
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#2


1  

const c = [{a: null}, {a: 12}, {a: 1}, {a: 50}, {a: 2}, {a: null}];

let result = [...c.filter(_=>_["a"]!==null),...c.filter(_=>_["a"]===null)];

console.log(result);

#3


1  

Try with return !a.a - !b.a .valid object goes first

试着返回!。——! b。首先是一个.valid对象

var c = [{a: null}, {a: 12}, {a: 1}, {a: 50}, {a: 2}, {a: null}];

c.sort(function (a, b) {
    return !a.a - !b.a
});
console.log(c);

#4


1  

That way ?

那条路?

c=[{a: null}, {a: 12}, {a: 1}, {a: 50}, {a: 2}, {a: null}];
c.sort(function(a,b){ return a.a===null ? 1:-1 })
console.log(c);

Edited

编辑

#5


1  

var c = [{
  a: null
}, {
  a: 12
}, {
  a: 1
}, {
  a: 50
}, {
  a: 2
}, {
  a: null
}];

c.sort(function(a, b) {
  return (a.a !== null) ? 0 : 1;
});
console.log(c);

Returning 0 in the sort function will keep the order as it is.

在排序函数中返回0将保持顺序不变。

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

如果compareFunction(a, b)返回0,将a和b保持不变,但对所有不同的元素进行排序。

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

#6


0  

It is not perfect but still works,cheers!

这不是完美的,但仍然有效,干杯!

var c = [{a: null}, {a: 12}, {a: 1}, {a: 50}, {a: 2}, {a: null}]

c.sort(function(object1,object2){

  
  if(object1.a === null && object2.a !== null){return 1}
  
  if([object1.a,object2.a].every((a)=>a === null) ||      
     [object1.a,object2.a].every((a)=>a !== null)
     ){return 0}
  
  if(object1.a !== null && object2.a === null){return -1}
  


})

console.log(c);

#1


2  

You could test the value. If null, then take the delta of the comparison.

你可以测试这个值。如果为零,则取比较的增量。

var c = [{ a: null }, { a: 12 }, { a: 1 }, { a: 50 }, { a: 2 }, { a: null }];

c.sort(function (a, b) {
    return (a.a === null) - (b.a === null);
});

console.log(c);
.as-console-wrapper { max-height: 100% !important; top: 0; }

For a stable sort, you could use sorting with map and use the indices as second sort option.

对于稳定排序,可以使用与map的排序并使用索引作为第二种排序选项。

// the array to be sorted
var list = [{ a: null }, { a: 12 }, { a: 1 }, { a: 50 }, { a: 2 }, { a: null }];

// temporary array holds objects with position and sort-value
var mapped = list.map(function(el, i) {
    return { index: i, value: el.a === null};
});

// sorting the mapped array containing the reduced values
mapped.sort(function(a, b) {
    return a.value - b.value || a.index - b.index;
});

// container for the resulting order
var result = mapped.map(function(el){
    return list[el.index];
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#2


1  

const c = [{a: null}, {a: 12}, {a: 1}, {a: 50}, {a: 2}, {a: null}];

let result = [...c.filter(_=>_["a"]!==null),...c.filter(_=>_["a"]===null)];

console.log(result);

#3


1  

Try with return !a.a - !b.a .valid object goes first

试着返回!。——! b。首先是一个.valid对象

var c = [{a: null}, {a: 12}, {a: 1}, {a: 50}, {a: 2}, {a: null}];

c.sort(function (a, b) {
    return !a.a - !b.a
});
console.log(c);

#4


1  

That way ?

那条路?

c=[{a: null}, {a: 12}, {a: 1}, {a: 50}, {a: 2}, {a: null}];
c.sort(function(a,b){ return a.a===null ? 1:-1 })
console.log(c);

Edited

编辑

#5


1  

var c = [{
  a: null
}, {
  a: 12
}, {
  a: 1
}, {
  a: 50
}, {
  a: 2
}, {
  a: null
}];

c.sort(function(a, b) {
  return (a.a !== null) ? 0 : 1;
});
console.log(c);

Returning 0 in the sort function will keep the order as it is.

在排序函数中返回0将保持顺序不变。

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

如果compareFunction(a, b)返回0,将a和b保持不变,但对所有不同的元素进行排序。

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

#6


0  

It is not perfect but still works,cheers!

这不是完美的,但仍然有效,干杯!

var c = [{a: null}, {a: 12}, {a: 1}, {a: 50}, {a: 2}, {a: null}]

c.sort(function(object1,object2){

  
  if(object1.a === null && object2.a !== null){return 1}
  
  if([object1.a,object2.a].every((a)=>a === null) ||      
     [object1.a,object2.a].every((a)=>a !== null)
     ){return 0}
  
  if(object1.a !== null && object2.a === null){return -1}
  


})

console.log(c);