-1

I have the following data structure:

var array = [
2016-11-24: Object,
2016-11-25: Object,
2016-11-26: Object,
2016-11-27: Object,
2016-11-28: Object
]

I want to sort the array from oldest data to newest but I'm having some trouble getting it to work. I been following this resource (Sort Javascript Object Array By Date) but the array won't sort.

I believe the object may be affecting this but I'm not sure of how to use sort() when arrays have objects inside of them.

Anything that I'm missing?

Edit:

More information. I'm building this through an .each() function that looks like this:

var retentionValues = [];

jQuery.each(results.values(), function( a, b ) {

        var retentionData = {};

        //Go deeper into the object under each data
         $.each(b, function(c, d){

                //Add People values into object
                if (c === "first") {
                  retentionData["People"] = d;
                } else { //Add values for specific days or weeks

                  //Go into object under counts
                  $.each(d, function(e, f){ 
                    retentionData["Retention Day " + e] = f;
                  })

                }

          })

        //Push dates into array and create object for data
        retentionValues[a] = retentionData;

      });

I need the key of the array to be the date since I'm passing this to another function but I need to sort the data before then.

Community
  • 1
  • 1
Ruben Ugarte
  • 55
  • 10
  • What does your array look like exactly? And what code have you tried? – Mottie Dec 23 '16 at 19:55
  • 1
    your array looks invalid. either it's an object with properties, then no sort can change the order or you have an array of object, then an order is possible. – Nina Scholz Dec 23 '16 at 19:55
  • @Mottie I tried using sort() and writing a short function to compare two values (a, b). – Ruben Ugarte Dec 23 '16 at 20:05
  • @NinaScholz What's the best way to display the structure of the actual array? Would a screenshot from the developer console work? – Ruben Ugarte Dec 23 '16 at 20:06
  • @RubenUgarte, you could use JSON.stringify with a spacer and display the string, you get in string form above. – Nina Scholz Dec 23 '16 at 20:08

3 Answers3

1

It looks like your array is not valid, as Nina Scholz said.

This is one of the ways how you can organize your data and sort it:

var array = [
  { date:'2016-11-24', obj: Object},
  { date:'2016-11-25', obj: Object},
  { date:'2016-11-22', obj: Object},
  { date:'2016-11-27', obj: Object},
  { date:'2016-11-28', obj: Object}
];

var sortedArr = array.sort(function (a, b) {
  return (new Date(a.date) > new Date(b.date))
});
0

Assuming a valid array with objects with a date property, you could treat an ISO 8601 date string as string, without converting to a date object, because it is directly sortable.

Array#some sorts in-situ, that means the original array is sorted.

var array = [{ date: '2016-11-24', obj: { pos: 0 } }, { date: '2016-11-25', obj: { pos: 1 } }, { date: '2016-11-22', obj: { pos: 2 } }, { date: '2016-11-27', obj: { pos: 3 } }, { date: '2016-11-28', obj: { pos: 4 } }];

array.sort(function (a, b) {
    return a.date.localeCompare(b.date);
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Partial Answer

Note that in JavaScript, arrays are made with the following syntax:

var array = [
item1,
item2,
...
];

To set up the array the way you want it, you can either make a 2D Array(see here for more details) like so:

var array = [
[2016-11-24, Object],
[2016-11-25, Object],
[2016-11-26, Object],
[2016-11-27, Object],
[2016-11-28, Object]
]

Or, alternatively, you can make an array with Objects as items, like this:

var array = [
{2016-11-24 : Object},
{2016-11-25 : Object},
{2016-11-26 : Object},
{2016-11-27 : Object},
{2016-11-28 : Object}
]
Community
  • 1
  • 1
TrojanByAccident
  • 227
  • 1
  • 6
  • 20