3

So I have this array of notification objects that has to be sorted in decreasing order of severity i.e. Error > Warning > Information.

Example:

var notificationArray = [ {
    code : "103",
    severity : "Error"
}, {
    code : "104",
    severity : "Information"
}, {
    code : "109",
    severity : "Error"
}, {
    code : "403",
    severity : "Warning"
}, {
    code : "510",
    severity : "Information"
}, {
    code : "114",
    severity : "Warning"
}, {
    code : "144",
    severity : "Error"
}, {
    code : "413",
    severity : "Warning"
} ];

What's the easiest way to make sure this array is always sorted based on severity?

P.S. There are other threads on sorting an array of objects but what I mostly found is unicode sorting and not sorting by comparing against a fixed value. Apologies if I posted a duplicate question.

2
  • “but I didn't find one that talks about sorting on attribute value (and not attribute itself)” – what else would you sort by, if not the value? Commented Mar 1, 2017 at 14:07
  • Possible duplicate of Sorting by properties by priority Commented Mar 1, 2017 at 14:09

3 Answers 3

15

You could sort with an order object for the priority of severity.

var notificationArray = [{ code: "103", severity: "Error" }, { code: "104", severity: "Information" }, { code: "109", severity: "Error" }, { code: "403", severity: "Warning" }, { code: "510", severity: "Information" }, { code: "114", severity: "Warning" }, { code: "144", severity: "Error" }, { code: "413", severity: "Warning" }, { code: "131", severity: "Error"}],
    order = { Error: 1, Warning: 2, Information: 3 };

notificationArray.sort(function (a, b) {
    return order[a.severity] - order[b.severity];
});

console.log(notificationArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

8 Comments

Would it not be better to define the order object outside the sort function ?
@Weedoze, better, hm. but why not.
I've upvoted your other (same) answer so that we can use it as a dupe target for those frequent questions...
@Nina, worked with the original set but then I introduced another {code: "131", severity: "Error"} in the array and tried to sort again. Didn't work!
@Weedoze, it uses the same reference, is suppose. it makes no difference, but some is local and the other global.
|
0

Here a solution:

var errors = notificationArray.map(function(message) {
  return message.severity === 'Error';
});
var warnings = notificationArray.map(function(message) {
  return message.severity === 'Warning';
});
var infos = notificationArray.map(function(message) {
  return message.severity === 'Information';
});

var sortedMessage = errors.concat(warnings).concat(infos);

I don't know if it's faster then @NinaScholz solution, (perhaps it's more understable for you).

Comments

-1

For each severity, associate a priority

EX :

var severity = {
    information: 0
    warning : 1,
    error : 2, 
}

Then sort the array based on the priority of each severity

notificationArray.sort(function(a, b) {
    var p1 = severity[a.severity.toLowerCase()];
    var p2 = severity[b.severity.toLowerCase()];
    return p2 - p1;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.