2

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

SBB
  • 7,079
  • 27
  • 82
  • 181
  • 2
    Objects in javascript are never guaranteed to be sorted. If order is important, you should use an array – Brennan May 02 '17 at 20:11

2 Answers2

4

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.

quirimmo
  • 9,161
  • 1
  • 24
  • 43
  • This would require them to store `noteDate` on the object inside the array instead of using it as the key in the array (which they should probably do anyway -- what if two notes have the same date?) – Will P. May 02 '17 at 20:20
  • date is in one property for sure. I just called it noteDate because I don't know the name of the property. Then using a key for an object, means the same as define an attribute. If dates are equal, it returns 0. This is how the comparison works in general. – quirimmo May 02 '17 at 20:24
0

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.

Community
  • 1
  • 1
splitwire
  • 185
  • 5