Janso123
Janso123

Reputation: 200

sort array multidimensional JavaScript

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 which version of JavaScript I'm working on?

Upvotes: 0

Views: 248

Answers (3)

trincot
trincot

Reputation: 351318

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; }

Upvotes: 1

Lennholm
Lennholm

Reputation: 7480

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.

Upvotes: 0

rpadovani
rpadovani

Reputation: 7360

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)

Upvotes: 2

Related Questions