Sort an arary of objects by date

Sort an arary of objects by date

By : user2949332
Date : November 16 2020, 06:23 AM
help you fix your problem I got an array of objects which I want to sort by date. I've got the date formatted like this with a property - created_at: "2014-09-26 19:27:43". , Use Date objects for comparison and Array.prototype.sort() to sort.
code :
var array = ["2014-09-26 19:27:43","2014-09-26 19:27:42","2014-09-23 19:27:43"];
var sortedArray = array.sort(function(a,b){
       return new Date(a) - new Date(b);

Share : facebook icon twitter icon
Sort array of objects with date field by date

Sort array of objects with date field by date

By : Oaddy Kittithat
Date : March 29 2020, 07:55 AM
will be helpful for those in need You don't really need lodash. Just use JavaScript's Array.prototype.sort method.
You'll need to create Date objects from your date strings before you can compare them.
code :
var myArray = [{
  name: "Joe Blow",
  date: "Mon Oct 31 2016 00:00:00 GMT-0700 (PDT)"
}, {
  name: "Sam Snead",
  date: "Sun Oct 30 2016 00:00:00 GMT-0700 (PDT)"
}, {
  name: "John Smith",
  date: "Sat Oct 29 2016 00:00:00 GMT-0700 (PDT)"

myArray.sort(function compare(a, b) {
  var dateA = new Date(a.date);
  var dateB = new Date(b.date);
  return dateA - dateB;

I have an array of objects and each object has been assigned a date, how can I sort these objects in order of date?

I have an array of objects and each object has been assigned a date, how can I sort these objects in order of date?

By : John Stack
Date : March 29 2020, 07:55 AM
To fix this issue If your looking for a simpler solution in java, then solution by user2004685 should do.
Since you've tagged bubble-sort, I'm guessing you need help with the issue in your code.
code :
for (int i = 0; i<peopleArray.size(); i++)
    for (int j = 0; j<peopleArray.size(); j++)
        LocalDate firstDate = LocalDate.of(Integer.parseInt(peopleArray.get(i).getDOBYear()), 

        LocalDate secondDate= LocalDate.of(Integer.parseInt(peopleArray.get(j).getDOBYear()), 

            Person temp = peopleArray[i];
            peopleArray[i] = peopleArray[j];
            peopleArray[j] = temp;
sort the list of objects for a record based on two dates(create date and update date)

sort the list of objects for a record based on two dates(create date and update date)

By : Dynu
Date : March 29 2020, 07:55 AM
This might help you You do not need to sort the list to get the most recent entry. Just use max:
code :
import static java.util.Comparator.comparing;

           c -> c.getUpdatedTime() == null ? c.getCreatedTime() : c.getUpdatedTime()
How to sort a list of objects by date when date is String?

How to sort a list of objects by date when date is String?

By : Valério Leite
Date : March 29 2020, 07:55 AM
this one helps. I think that you can create a custom comparator with an structure similar to the below:
code :
Collections.sort(datestring, new Comparator<String>() {
    DateFormat df = new SimpleDateFormat("your format");
    public int compare(String s1, String s2) {
       try {
            return df.parse(s1).compareTo(df.parse(s2));
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
How to sort objects in an array by date without using the sort method?

How to sort objects in an array by date without using the sort method?

By : user3653724
Date : March 29 2020, 07:55 AM
wish helps you For a project I have to do for school, I have to make an application that can sort appointments by date and time. I have an array with objects in them but can't figure out how to sort it by date as the date is nested. , Try This
code :
function bubbleSort() {
    const loop = listOfAppointments.length;

    for(let i = 0; i < loop; i++) {
        for(let j = i+1; j < loop; j++) {
            if(new Date(listOfAppointments[i].Appointment.time) > new Date(listOfAppointments[j].Appointment.time)) {
                let temp = listOfAppointments[i];
                listOfAppointments[i] = listOfAppointments[j];
                listOfAppointments[j] = temp;

Related Posts Related Posts :
  • How to change add and remove active class in JavaScript
  • how to toggle a specific div in javascript
  • How to get all the values from multiple keys of an array of object?
  • jQuery bind() unbind() and on() and off()
  • regular expression to find DD Month YYYY from a string
  • How to upload files from web client (HTML/jQuery form) to your Dropbox folder
  • Backbone history and require.js issue
  • Confusion over Classical and Prototypal Inheritance Javascript
  • How to take screen shot of current webpage using Javascript/JQuery?
  • Store meridiem in a variable
  • class declared inside closure vs standard class without closure
  • javascript confirm box always return true
  • dynamically changing the size of font size based on text length using css and html
  • Node.js - Getting the host IP address while on Amazon EC2
  • Confusing CSS, can anyone explain please
  • How to install v8-profiler on Windows 8 (64 bit) using npm?
  • Resolve promise based on another promise
  • google maps adding overlay layer above UI and markers
  • Style specific letter?
  • RangeError: Maximum call stack size exceeded with array.slice
  • node rest client get with local variable
  • If condition not executes while mouse move fastly
  • Count the number of displayed elements in a HTML list
  • Locate JavaScript source code in Emacs
  • Clean, Modular Code vs MV* Frameworks
  • infinite scroll without loading image
  • Backbone: reverse collection order with comparator
  • What do printers ignore?
  • jQuery UI .tabs() Contentless tab?
  • Execute Javascript alert() after page (visually) loaded
  • JavaScript - duplicating array doesn't work
  • Excessive clickable area below image
  • JavaScript Regex: Replace |b| with <b>
  • Unexpected value change in 2D array in JavaScript
  • Function doesnt see parameter as a array of objects
  • jQuery fetch keypress event on chrome and IE
  • How to enable jquery validate localization?
  • Cassandra map collection returned by node.js Helenus looks odd
  • angular ng-repeat with multiple filter options
  • Selecting Children without ID or Class names
  • How to uncheck a group of checkboxes when another checkbox is checked
  • Is hiding content by Javascript or jQuery Worth trying
  • Load XUL resource using javascript
  • XML to HTML text area from server file system
  • set focus() on textbox on form onload
  • es lint '' is assigned a value but never used' eventhough i have used it
  • Can "name" attribute be used for custom VueJS components
  • Get innerHTML of content when it is clicked
  • HTTPS causes CSS animations to not load? Very confused
  • How to hide other div apart from touched div to the band
  • Is global variable assignment atomic on NodeJS?
  • Datepicker onchangemonthyear beforeshowday
  • trying to render html files using jade but it still adresses it as a jade
  • Google Analytics - Understanding and Dissecting the Snippet: What is | | [ ];
  • Replacing "[aA09.b]." to "[aA09.b]\n" in in JavaScript
  • Why wrap a function definition in an immediate function?
  • Enable / disable jQuery script on switching to different tab in a HTML page
  • how can find return variable value outside anonymous function in node js mysql query function
  • Converting 1 to 0001 in javascript array
  • svg viewbox should not resize the text fontSize?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org