I have an array of objects where I would like to sort by a specific property; however, if two objects have the same first property value then I would need to sort those objects by a specific ID value.
I would like to sort the below array via the priority property (lowest to highest) and if two objects have the same priority to then sort the objects via the ID value (highest to lowest).
Is something like this possible with using Underscores _.groupBy or _.sortBy functions??
[
{
"id":100,
"priority":99,
"summary":"Standard Boring Message",
"title":"Low priority boring old message",
"text":"This is where logs ot <h2>Html</h2> and <b>stuff</b> can be displayed",
"cssClass":"nb-message-99",
"isExpanded":false,
"hasSummary":true,
"hasTitle":true,
"hasText":true,
"canExpand":true,
"toggleExpandMessageText":"Learn More"
},
{
"id":101,
"priority":3,
"summary":"Template test",
"cssClass":"nb-message-3",
"subTemplateName":"notification-error-template",
"subTemplateData":{
"errorId":12345667,
"errorMessage":"this is the error data"
},
"isExpanded":false,
"hasSummary":true,
"hasSubTemplate":true,
"canExpand":true,
"toggleExpandMessageText":"Learn More"
},
{
"id":102,
"priority":4,
"summary":"Everything is green",
"title":"All across the board",
"text":"Nothing to see here",
"cssClass":"nb-message-4",
"dismissalId":-1,
"isExpanded":false,
"hasSummary":true,
"hasTitle":true,
"hasText":true,
"canExpand":true,
"toggleExpandMessageText":"Learn More"
},
{
"id":103,
"priority":1,
"summary":"Unable To load data",
"title":"This is the title",
"text":"There is some text",
"cssClass":"nb-message-1",
"dismissalId":-1,
"isExpanded":false,
"hasSummary":true,
"hasTitle":true,
"hasText":true,
"canExpand":true,
"toggleExpandMessageText":"Learn More"
},
{
"id":104,
"priority":3,
"summary":"There is a new release!",
"title":"New stuff!",
"text":"New and improved! All bits have been pre-washed and are now cleaner than the old bits",
"cssClass":"nb-message-3",
"dismissalId":-1,
"isExpanded":false,
"hasSummary":true,
"hasTitle":true,
"hasText":true,
"canExpand":true,
"toggleExpandMessageText":"Learn More"
},
{
"id":105,
"priority":2,
"summary":null,
"title":"This is the title with no summary",
"text":"There is some more text",
"cssClass":"nb-message-2",
"dismissalId":-1,
"isExpanded":false,
"hasSummary":null,
"hasTitle":true,
"hasText":true,
"canExpand":true,
"toggleExpandMessageText":"Learn More"
}
]
I've tried using the following but the array doesn't get sorted at all.
_.sortBy(self.messages(), 'priority');