admin管理员组

文章数量:1435859

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 bined 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 binedOutput As it stands, the notes are in their correct date order per note type. However, when it es time to bined 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 = [],
    binedOutput = [],
    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(binedOutput[noteDate]) == 'undefined') {
        binedOutput[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
    binedOutput[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 binedOutput by its key, which happens to be a date object.

This is a screenshot of how the binedOutput array looks right now with no sorting.

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 bined 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 binedOutput As it stands, the notes are in their correct date order per note type. However, when it es time to bined 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 = [],
    binedOutput = [],
    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(binedOutput[noteDate]) == 'undefined') {
        binedOutput[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
    binedOutput[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 binedOutput by its key, which happens to be a date object.

This is a screenshot of how the binedOutput array looks right now with no sorting.

Share Improve this question asked May 2, 2017 at 20:06 SBBSBB 8,99035 gold badges118 silver badges235 bronze badges 1
  • 2 Objects in javascript are never guaranteed to be sorted. If order is important, you should use an array – Brennan Commented May 2, 2017 at 20:11
Add a ment  | 

2 Answers 2

Reset to default 5

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

https://developer.mozilla/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.

Here is a Greate Answer.

Basically use sort() and inside it convert your dates into Date Objects and do a parison. 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.

本文标签: Javascript sort array by date keyStack Overflow