Reputation: 1776
I have the following array:
[{"cod_nivel":"INC2","cod_modelo":"D"},
{"cod_nivel":"PRIM1","cod_modelo":"B"},
{"cod_nivel":"INC2","cod_modelo":"B"},
{"cod_nivel":"INC1","cod_modelo":"D"},
{"cod_nivel":"PRIM1","cod_modelo":"D"},
{"cod_nivel":"BAC2","cod_modelo":"B"},
{"cod_nivel":"BAC2","cod_modelo":"D"},
{"cod_nivel":"BAC2","cod_modelo":"A"}]
I need to order this array of objects by "cod_modelo" ascending grouped by "cod_nivel". So the result should be:
[{"cod_nivel":"INC1","cod_modelo":"D"},
{"cod_nivel":"INC2","cod_modelo":"B"},
{"cod_nivel":"INC2","cod_modelo":"D"},
{"cod_nivel":"PRIM1","cod_modelo":"B"},
{"cod_nivel":"PRIM1","cod_modelo":"D"},
{"cod_nivel":"BAC2","cod_modelo":"A"},
{"cod_nivel":"BAC2","cod_modelo":"B"},
{"cod_nivel":"BAC2","cod_modelo":"D"}]
Ok I made this code that orders the array first by cod_nivel and then by cod_modelo:
var sortedArray = array.sort(function (a, b) {
return (a["cod_nivel"] > b["cod_nivel"]) ? 1 : -1;
}).sort(function (a, b) {
if (a["cod_nivel"] == b["cod_nivel"])
return (a["cod_modelo"] > b["cod_modelo"]) ? 1 : -1;
else
return 0;
});
The thing is that this code is ordering also by "cod_nivel" so the obtained array would be:
[{"cod_nivel":"BAC2","cod_modelo":"A"},
{"cod_nivel":"BAC2","cod_modelo":"B"},
{"cod_nivel":"BAC2","cod_modelo":"D"},
{"cod_nivel":"INC1","cod_modelo":"D"},
{"cod_nivel":"INC2","cod_modelo":"B"},
{"cod_nivel":"INC2","cod_modelo":"D"},
{"cod_nivel":"PRIM1","cod_modelo":"B"},
{"cod_nivel":"PRIM1","cod_modelo":"D"}]
Note that BAC2 cod_nivel objects are at the beginning.
What I need is to order first by cod_nivel but in a given order, which is:
I assume I need an array with the fixed order of "cod_nivel" and use it while ordring but I don't know how to use it so I did not include in my approach.
var order_arr = ['INC1', 'INC2', 'PRIM1', 'PRIM2', 'BAC1', 'BAC2']
And after that, order then by cod_modelo (grouped by each cod_nivel).
I hope I have explained myself clearly and someone can help me.
Upvotes: 3
Views: 4285
Reputation: 386520
var data = [{
"cod_nivel": "INC2",
"cod_modelo": "B"
}, {
"cod_nivel": "INC2",
"cod_modelo": "D"
}, {
"cod_nivel": "INC2",
"cod_modelo": "B"
}, {
"cod_nivel": "PRIM1",
"cod_modelo": "B"
}, {
"cod_nivel": "INC2",
"cod_modelo": "B"
}, {
"cod_nivel": "INC1",
"cod_modelo": "D"
}, {
"cod_nivel": "INC2",
"cod_modelo": "B"
}, {
"cod_nivel": "PRIM2",
"cod_modelo": "D"
}, {
"cod_nivel": "BAC2",
"cod_modelo": "B"
}, {
"cod_nivel": "BAC2",
"cod_modelo": "D"
}, {
"cod_nivel": "BAC2",
"cod_modelo": "A"
}];
var sortOrder = ['INC1', 'INC2', 'PRIM1', 'PRIM2', 'BAC1', 'BAC2'];
data.sort(function (a, b) {
// first have a look at cod_nivel
// get the indices of the given sortOrder
// take the difference of the indices
var delta = sortOrder.indexOf(a.cod_nivel) - sortOrder.indexOf(b.cod_nivel);
// test, if the indices are the same
if (delta === 0) {
// the comparison should usually return 0,
// but this will here not work. we need
// the comparison of the second sort order of cod_modelo.
// take the variables and make a real comparison,
// while we expecting strings to be compared
return a.cod_modelo === b.cod_modelo ? 0 : (a.cod_modelo < b.cod_modelo ? -1 : 1);
}
// otherwise return the delta
return delta;
});
as thefortheye mentioned, the former version does not compute very well. so here is the type safe version for number/number or string/string comparison.
Upvotes: 3
Reputation: 239443
You can assign indexes in an associative array, like this
var cod_nivel_order = {
'INC1': 0,
'INC2': 1,
'PRIM1': 2,
'PRIM2': 3,
'BAC1': 4,
'BAC2': 5
};
and then you can sort like this
function compare(a, b) {
if (a === b) {
return 0;
}
return a < b ? -1 : 1;
}
var sortedArray = array.sort(function (a, b) {
// First compare corresponding values of `cod_nivel` from `cod_nivel_order`
var index_result = compare(cod_nivel_order[a.cod_nivel],
cod_nivel_order[b.cod_nivel]);
// If they are equal
if (index_result === 0) {
// Return the result of comparing `cod_modelo`s
return compare(a.cod_modelo, b.cod_modelo);
}
return index_result;
});
and now the result will be
[ { cod_nivel: 'INC1', cod_modelo: 'D' },
{ cod_nivel: 'INC2', cod_modelo: 'B' },
{ cod_nivel: 'INC2', cod_modelo: 'D' },
{ cod_nivel: 'PRIM1', cod_modelo: 'B' },
{ cod_nivel: 'PRIM1', cod_modelo: 'D' },
{ cod_nivel: 'BAC2', cod_modelo: 'A' },
{ cod_nivel: 'BAC2', cod_modelo: 'B' },
{ cod_nivel: 'BAC2', cod_modelo: 'D' } ]
Upvotes: 10