Object and array sorting
by amindunited
JavaScript
var q_refs = [
{
index: 5,
value: 'this.queue_severe'
},
{
index: 4,
value: 'this.queue_high'
},
{
index: 3,
value: 'this.queue_medium'
},
{
index: 2,
value: 'this.queue_low'
},
{
index: 5,
value: 'A new sever'
},
{
index: 5,
value: 'An other!!!!'
}
];
/*
SORT the array by value
*/
var new_refs = q_refs.sort(q_refs_sorter);
function q_refs_sorter(a, b){
if (a.index < b.index) {
return -1
}
if (a.index > b.index) {
return 1
}
return 0;
}
console.log("new _refs ", new_refs);
/*
Get all the entries with a given value
*/
function getByPriority(val, arr){
var result = [];
for (var i in arr) {
if (arr.hasOwnProperty(i)) {
if (arr[i].index == val) {
result.push(arr[i]);
}
}
}
return result;
}
var fives = getByPriority(5, new_refs);
console.log("...", $(fives).first());
console.log("...", fives[0]);
console.log("FIVEs", fives);
var old_q_refs = {
"5": 'this.queue_severe',
"4": 'this.queue_high',
"3": 'this.queue_medium',
"2": 'this.queue_low'
};
console.log("old_q_refs", old_q_refs);