0

I have to sort this: I'm taking data from a db from two different select query and I have to sort them by mpriority and by mdate.

I know how to sort array of objects, but I cannot use object nor object.key

    var allocations = new Array(); 
    allocations[0] =
    [   qtA =  15,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp=  11,
        mpriority=  5,
        mdate=  new Date('2017-04-27 11:26:02.147'),
    ];

allocations[1] =
    [
        qtA= 13,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 5,
        mdate= new Date('2016-10-07 00:00:00.000'),
    ];

allocations[2] =
    [
        qtA= 16,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 4,
        mdate= new Date('2017-04-27 11:26:02.147'),
    ];

allocations[3] =
    [
        qtA= 95,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 1,
        mdate= new Date('2016-10-06 00:00:00.000'),
    ];

allocations[4] =
    [
        qtA= 75,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 8,
        mdate= new Date('2017-02-20 12:41:34.903'),
    ];

allocations[5] =
    [
        qtA= 45,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 0,
        mdate= new Date('2017-04-27 11:26:02.147'),
    ];

The sort function should look at first at mpriority - if two value are the same then check mdate

I think it would be something like this

var sortOptions = {
    byPriorityAndDate: function (a, b) {
        return (b.mpriority - a.mpriority) || (a.mdate - b.mdate);
    },
}

and how to check wich version of js i'm working on ?

rpadovani
  • 7,101
  • 2
  • 31
  • 50
Janso123
  • 192
  • 1
  • 12
  • What is the problem you encounter with your sort function? NB: (1) The syntax of your array definition is not doing what you think it does. (2) for knowing the version of JavaScript, read what the version release of your browser says about that. See also [this simple check](http://stackoverflow.com/a/4271622/5459839) – trincot May 11 '17 at 16:04
  • Is this pseudo-code or something? Because it looks like you're trying to create an object rather than an array – Lennholm May 11 '17 at 16:12

3 Answers3

2

You have array of arrays, not of objects.

I suggest you to convert your array of arrays in an array of objects, instantiating every element like this:

allocations[0] = {
    qtA:  15,
    mdata:  '1234',
    mid:  '234234234',
    qtyToDoTemp:  11,
    mpriority:  5,
    mdate:  new Date('2017-04-27 11:26:02.147'),
};

In this way the function you have written for comparison should work

Also, I suggest you to read Compare two dates with JavaScript to have an idea of which kind of comparison you want

Anyway, this sort function will work for your case:

allocations = allocations.sort((a, b) => {
   if (a[4] === b[4]) {
        return a[5].getTime() - b[5].getTime();
   }

   return a[4] - b[4];
})

var allocations = new Array(); 
    allocations[0] =
    [   qtA =  15,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp=  11,
        mpriority=  5,
        mdate=  new Date('2017-04-27 11:26:02.147'),
    ];

allocations[1] =
    [
        qtA= 13,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 5,
        mdate= new Date('2016-10-07 00:00:00.000'),
    ];

allocations[2] =
    [
        qtA= 16,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 4,
        mdate= new Date('2017-04-27 11:26:02.147'),
    ];

allocations[3] =
    [
        qtA= 95,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 1,
        mdate= new Date('2016-10-06 00:00:00.000'),
    ];

allocations[4] =
    [
        qtA= 75,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 8,
        mdate= new Date('2017-02-20 12:41:34.903'),
    ];

allocations[5] =
    [
        qtA= 45,
        mdata=  '1234',
        mid =  '234234234',
        qtyToDoTemp= 11,
        mpriority= 0,
        mdate= new Date('2017-04-27 11:26:02.147'),
    ];

allocations = allocations.sort((a, b) => {
   if (a[4] === b[4]) {
        return a[5].getTime() - b[5].getTime();
   }
   
   return a[4] - b[4];
})

console.log(allocations)
Community
  • 1
  • 1
rpadovani
  • 7,101
  • 2
  • 31
  • 50
1

The way you initialise your array is wrong. Although it will run without errors, it does something different than you expect.

This:

allocations[0] =
[   qtA =  15,
    mdata=  '1234',
    mid =  '234234234',
    qtyToDoTemp=  11,
    mpriority=  5,
    mdate=  new Date('2017-04-27 11:26:02.147'),
];

...will in fact assign values to global variables, like this:

qtA =  15,
mdata=  '1234',
mid =  '234234234',
qtyToDoTemp=  11,
mpriority=  5,
mdate=  new Date('2017-04-27 11:26:02.147')

And then the array will get at index 0 a nested array with the following values:

allocations[0] = [
    15,
    '1234',
    '234234234',
    11,
    5,
    new Date('2017-04-27 11:26:02.147'),
];

So, the result of your code is an array of nested arrays, not of objects.

To create an array of objects, use this syntax:

var allocations = [{
    qtA: 15,
    mdata:  '1234',
    mid:  '234234234',
    qtyToDoTemp:  11,
    mpriority:  5,
    mdate:  new Date('2017-04-27 11:26:02.147'),
}, {
    qtA: 13,
    mdata:  '1234',
    mid :  '234234234',
    qtyToDoTemp: 11,
    mpriority: 5,
    mdate: new Date('2016-10-07 00:00:00.000'),
}, {
    // ...etc, etc
}];

And now your function will work:

var sortOptions = {
    byPriorityAndDate: function (a, b) {
        return (b.mpriority - a.mpriority) || (a.mdate - b.mdate);
    },
}

var allocations = [{
    qtA: 15,
    mdata:  '1234',
    mid:  '234234234',
    qtyToDoTemp:  11,
    mpriority:  5,
    mdate:  new Date('2017-04-27 11:26:02.147'),
}, {
    qtA: 13,
    mdata:  '1234',
    mid :  '234234234',
    qtyToDoTemp: 11,
    mpriority: 5,
    mdate: new Date('2016-10-07 00:00:00.000'),
}, {
    qtA: 16,
    mdata:  '1234',
    mid :  '234234234',
    qtyToDoTemp: 11,
    mpriority: 4,
    mdate: new Date('2017-04-27 11:26:02.147'),
}, {
    qtA: 95,
    mdata:  '1234',
    mid :  '234234234',
    qtyToDoTemp: 11,
    mpriority: 1,
    mdate: new Date('2016-10-06 00:00:00.000'),
}, {
    qtA: 75,
    mdata:  '1234',
    mid :  '234234234',
    qtyToDoTemp: 11,
    mpriority: 8,
    mdate: new Date('2017-02-20 12:41:34.903'),
}, {
    qtA: 45,
    mdata:  '1234',
    mid:  '234234234',
    qtyToDoTemp: 11,
    mpriority: 0,
    mdate: new Date('2017-04-27 11:26:02.147'),
}];

allocations.sort(sortOptions.byPriorityAndDate);

console.log(allocations);
.as-console-wrapper { max-height: 100% !important; top: 0; }
trincot
  • 317,000
  • 35
  • 244
  • 286
0

OK, here I've rewritten your code so that it does what I think you want it to do:

var allocations = []; 
allocations.push({
  qtA: 15,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 5,
  mdate: new Date('2017-04-27 11:26:02.147')
});

allocations.push({
  qtA: 13,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 5,
  mdate: new Date('2016-10-07 00:00:00.000')
});

allocations.push({
  qtA: 16,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 4,
  mdate: new Date('2017-04-27 11:26:02.147')
});

allocations.push({
  qtA: 95,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 1,
  mdate: new Date('2016-10-06 00:00:00.000')
});

allocations.push({
  qtA: 75,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 8,
  mdate: new Date('2017-02-20 12:41:34.903')
});

allocations.push({
  qtA: 45,
  mdata: '1234',
  mid: '234234234',
  qtyToDoTemp: 11,
  mpriority: 0,
  mdate: new Date('2017-04-27 11:26:02.147')
});

If you're unfamiliar with Javascript and come from a different language, know this: arrays in JS are not like arrays in other languages, they're objects that have special syntax and do their best to behave like arrays.

Lennholm
  • 7,205
  • 1
  • 21
  • 30