SBB
SBB

Reputation: 8970

Javascript sort array by date key

I have a javascript loop that is iterating over some notes in a database and concatenating them into a string to append to the DOM. It is using the noteType as the key so I can separate the different types of notes on multiple tabs.

The issue is, I need to show a combined view of all notes sorted by date.So at the end of the loop, I append the notes to a separate variable that I call combinedOutput As it stands, the notes are in their correct date order per note type. However, when it comes time to combined them all as one, two sets of correctly ordered arrays are being appended in their respective order and throwing off the date order in the final output.

Here is an example of my code:

// Define our vars
var output = [],
    combinedOutput = [],
    noteType = '',
    noteDate = new Date;

$(data).find('notes>data').each(function() {

    var $p = $(this);

    noteType = $p.find('noteType').text(),
    noteDate = moment.utc($p.find('timestampOrig').text()).toDate()

    if (typeof(output[noteType]) == 'undefined') {
        output[noteType] = "";
    }
    if (typeof(combinedOutput[noteDate]) == 'undefined') {
        combinedOutput[noteDate] = new Date;
    }

    // Create our note
    output[noteType] += '<div id="message_' + $p.find('recordID').text() + '" class="panel panel-default custom-panel item">';
    output[noteType] += 'Something Here';
    output[noteType] += '</div>';

    // Append to our final output variable
    combinedOutput[noteDate] += output[noteType];

});

Dates Example

**Note Type: Public**
April 1, 2017
March 5, 2017
April 8, 2017

**Note Type: Private**
April 2, 2017
March 9, 2017
March 11, 2017

**Combined Notes:**
April 1, 2017
March 5, 2017
April 8, 2017
April 2, 2017
March 9, 2017
March 11, 2017

My end goal here is to sort the combinedOutput by its key, which happens to be a date object.

This is a screenshot of how the combinedOutput array looks right now with no sorting. enter image description here

Upvotes: 3

Views: 3646

Answers (2)

quirimmo
quirimmo

Reputation: 9988

You can use the Array.sort() method. Passing a function you can specify how to compare values. Here the link to the sort method:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort?v=example

So, in your example, get the data from the server inside an array. Then you can order them in this way: You can have something like this with your output:

resultArray.sort(function(a, b) {
  var dateA = new Date(a.noteDate);
  var dateB = new Date(b.noteDate);

  if (dateA < dateB ) {
    return -1;
  }
  if (dateA > dateB ) {
    return 1;
  }
  return 0;
});

Then after you have your data sorted, repeat your procedure for iterating over them and display values. You should already have them ordered.

Upvotes: 5

splitwire
splitwire

Reputation: 297

Here is a Greate Answer.

Basically use sort() and inside it convert your dates into Date Objects and do a comparison. The link will show you how this is done. It would be a quicker sort if you added a date object to the stored object in the array so you would not have to construct and discard so many objects at sort time.

Upvotes: 0

Related Questions