Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Sorting an array object by property having falsy value - JavaScript
Suppose, we have an array of objects like this ?
const array = [
{key: 'a', value: false},
{key: 'a', value: 100},
{key: 'a', value: null},
{key: 'a', value: 23}
];
We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property.
Understanding Falsy Values
In JavaScript, falsy values include false, null, undefined, 0, "", and NaN. However, we need to be careful with the number 0 since it might be a valid value in some contexts.
Solution Approach
Our solution uses a custom comparison function that:
- Identifies falsy values (excluding zero for numbers)
- Pushes falsy values to the bottom
- Sorts truthy values in descending order
Example
Following is the code ?
const arr = [
{key: 'a', value: false},
{key: 'a', value: 100},
{key: 'a', value: null},
{key: 'a', value: 23}
];
const isValFalsy = (obj) => !obj.value && typeof obj.value !== 'number';
const sortFalsy = arr => {
arr.sort((a, b) => {
if(isValFalsy(a) && isValFalsy(b)){
return 0;
}
if(isValFalsy(a)){
return 1;
};
if(isValFalsy(b)){
return -1;
};
return b.value - a.value;
});
};
sortFalsy(arr);
console.log(arr);
Output
This will produce the following output in console ?
[
{ key: 'a', value: 100 },
{ key: 'a', value: 23 },
{ key: 'a', value: false },
{ key: 'a', value: null }
]
How It Works
The isValFalsy function checks if a value is falsy but excludes numbers (including 0) from being considered falsy. The sorting function:
- Returns 0 when both values are falsy (maintains relative order)
- Returns 1 to push falsy values down
- Returns -1 to keep truthy values up
- Uses
b.value - a.valuefor descending numeric sort
Alternative Implementation
Here's a more readable version using separate arrays:
const arr2 = [
{key: 'a', value: false},
{key: 'a', value: 100},
{key: 'a', value: null},
{key: 'a', value: 23},
{key: 'a', value: 0}
];
const sortByFalsyValues = (array) => {
const truthyValues = [];
const falsyValues = [];
array.forEach(obj => {
if (!obj.value && typeof obj.value !== 'number') {
falsyValues.push(obj);
} else {
truthyValues.push(obj);
}
});
// Sort truthy values in descending order
truthyValues.sort((a, b) => b.value - a.value);
return [...truthyValues, ...falsyValues];
};
console.log(sortByFalsyValues(arr2));
[
{ key: 'a', value: 100 },
{ key: 'a', value: 23 },
{ key: 'a', value: 0 },
{ key: 'a', value: false },
{ key: 'a', value: null }
]
Conclusion
Both approaches effectively sort arrays by pushing falsy values to the bottom while maintaining descending order for truthy values. The first method modifies the original array, while the second creates a new sorted array.
