Warning: session_start(): open(/tmp/sess_fe72e500dfb2d72f5c59fcdbc0681a2e, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Sort An Array Of Objects By Boolean Property In JavaScript - LearnShareIT

How To Sort An Array Of Objects By Boolean Property In JavaScript

Sort an Array of Objects by Boolean property in JavaScript

In JavaScript, we often use the sort method to sort the array for a property with a Boolean value. We can also sort an array of objects by boolean property in JavaScript.

Sort an srray of objects by boolean property in JavaScript

To continue with this article, I will introduce you to the sort() method, which is used to sort the elements of the array in the order we want.

Syntax:

array.sort(compareFunction)

Parameter:

  • compareFunction: This function helps us determine the order of elements in an array. According to the arguments we pass, this function must return a negative, 0, or positive number. The sort order is (negative, 0, and positive).

Example:

var nums = [3, 5, 8, 2, 1, 4];

// Sort array nums 
nums.sort(function(a, b) {
	return a - b
});

console.log(nums);

Output:

(6) [1, 2, 3, 4, 5, 8]

The above example shows that the original function has been sorted in ascending order.

After learning the sort() method, we see that to sort an array. We have to determine the compareFunction of the problem. Below are the two ways I built compareFunction to implement sort an array of objects by boolean property.

Use conditional statements

We can use the if conditional statement to check and give the appropriate return value to fulfill the request in this article.  

Syntax:

if (condition) {
  // Code that executes if condition is true
}

To sort an array of objects by the boolean property, I will build it with the conditional statement as follows:

var arr = [{
	isActive: true
}, {
	isActive: false
}, {
	isActive: true
}, {
	isActive: false
}, {
	isActive: false
}, {
	isActive: true
}];

// Sort array arr
arr.sort(function(x, y) {
	// True values first
	if (x.isActive === y.isActive) {
		return 0;
	} else {
		if (x.isActive)
			return -1;
		else
			return 1;
	}
});

console.log(arr);

Output:

[
  { isActive: true },
  { isActive: true },
  { isActive: true },
  { isActive: false },
  { isActive: false },
  { isActive: false }
]

I was outputting the results before and after sorting in the above code. I sort the array in order of actual before and false after. If I wanted to sort by false, I did it in the comment code in the above example.

Use toLocaleDateString() method

(-) property

// Sort array arr
arr.sort(function(a, b) {

// True values first
	return a.isActive - b.isActive
});

console.log(arr);

Next is the Conditional Operator:

arr.sort(function(a, b) {

//  True values first
	  return (x === y) ? 0 : x ? -1 : 1;
});

console.log(arr);

Finally, the combination of (<), (*), (-) operator:

arr.sort(function(a, b) {
// True values firstreturn 
	(a.isActive < b.isActive) * 2 - 1
	
// False values first
	return (a.isActive > b.isActive) * 2 - 1
});

console.log(arr);

All the above ways have the same output:

[
  { isActive: false },
  { isActive: false },
  { isActive: false },
  { isActive: true },
  { isActive: true },
  { isActive: true }
]

Summary

I showed you two ways to sort an array of objects by boolean property in JavaScript. Try following me. It will help you remember the above ways and understand them better. I hope this article helps you and your program. Good luck.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *