Warning: session_start(): open(/tmp/sess_11b5b94b52e3c61282953761683bbde5, 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 Date Property In JavaScript - LearnShareIT

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

Sort an Array of Objects by Date property in JavaScript

This tutorial will help you learn about how to sort an array of objects by date property using JavaScript. Javascript offers you some methods to do it. We already tested effectively by using the sort() function with the Date() object. Hopefully, throughout this article, you can find for yourself the best way to sort an array of objects by date. Read till the end of the article to see how we use it.

To Sort An Array Of Objects By Date Property In JavaScript

Using The Array sort() method

JavaScript provides the sort() function to sort the object, which allows developers to put the
Date objects in an array of objects and arranges the dates in any order. The sort() function will help us sort the elements of an array and will overwrite the original array. To more detail about this function. We already have an article that discusses how to utilize this function in further depth. Please read it here.

Code Example

In this example, we assort an array of objects by date property following this way.

  1. First, we create an array of an object named “schedule” that stores object containing Date objects.
  2. We subtract two date objects.
  3. After that, call out the sort() method on an array.

Let’s see the code example below.

const schedule = [
    { id: 1, date: new Date("2022-12-23"), title: "Meeting with customer" },
    { id: 2, date: new Date("2022-12-17"), title: "Go to shopping" },
    { id: 3, date: new Date("2022-12-27"), title: "Go to fishing" },
];

// Using the sort() function
schedule.sort(function (a, b) {
    // subtract the date value
    return b.date - a.date;
});

console.log("After sorting by date in descending order", schedule);

Output

After sorting by date
(3) [{...}, {...}, {...}]
0:(3) {id: 3, date: Tue Dec 27 2022 07:00:00 GMT, title: "Go to shopping"}
1:(3) {id: 1, date: Fri Dec 23 2022 07:00:00 GMT, title: "Meeting with customer"}
2:(3) {id: 2, date: Sat Dec 17 2022 07:00:00 GMT, title: "Go to fishing"}

Create the customSort with the sort() method

The second way we want to introduce this article is by creating the customSort function. In this function, we will create the two date object and compare them together. Then, we use the sort() method with customSort function to organize all elements in an array of the object by using the Date() object.

Code Example

const schedule = [
    { id: 1, date: new Date("2022-12-23"), title: "Meeting with customer" },
    { id: 2, date: new Date("2022-12-17"), title: "Go to shopping" },
    { id: 3, date: new Date("2022-12-27"), title: "Go to fishing" },
];

// Create customSort to sort descending order
customSort = (a, b) => {
    const date1 = new Date(a.date);
    const date2 = new Date(b.date);

    if (date1 > date2) {
        return -1;
    } else if (date1 < date2) {
        return 1;
    } else {
        return 0;
    }
};

console.log(
  "After sorting by date in descending order",
  schedule.sort(customSort)
);

Output

After sorting by date
(3) [{...}, {...}, {...}]
0:(3) {id: 3, date: Tue Dec 27 2022 07:00:00 GMT, title: "Go to shopping"}
1:(3) {id: 1, date: Fri Dec 23 2022 07:00:00 GMT, title: "Meeting with customer"}
2:(3) {id: 2, date: Sat Dec 17 2022 07:00:00 GMT, title: "Go to fishing"}

Summary

To summarize, you will be an expert in sorting an array of objects by date property in Javascript after using the two techniques I’ve described called the sort() function in an array and subtracting the two date objects. If you have any comments on the article, leave me a comment below.

Thank you for your reading!

Maybe you are interested:

Leave a Reply

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