I am trying to sort an array without it being alphabetical. In my case I want the order of the messages returned to be error, warning and then info. Reading about this at W3 I found a similar example...
They give this example with cars but have changed it too messages for my use case.
var message = [
{type:"Error", message:"This is an Error"},
{type:"Info", message:"This is an Info"},
{type:"Warning" message:"This is a Warning"}
];
function myFunction() {
message.sort(function(a, b){
var x = a.type.toLowerCase();
var y = b.type.toLowerCase();
if (x < y) {return -1;}
if (x > y) {return 1;}
return 0;
});
}
I thought this was all about scoring the value so I thought this might be right way to do it but it isnt giving me the expected results...
function myFunction() {
message.sort(function(a, b){
var x = a.type.toLowerCase();
var y = b.type.toLowerCase();
if (x === 'error') {return 1;}
if (x === 'warning') {return 2;}
if (x === 'info') {return 3;}
return 0;
});
}