我有一个对象数组,我希望按特定属性排序;但是,如果两个对象具有相同的第一个属性值,那么我需要按特定的ID值对这些对象进行排序。
我想通过优先级属性(从最低到最高)对下面的数组进行排序,如果两个对象具有相同的优先级,则通过ID值(从最高到最低)对对象进行排序。
使用Underscores _.groupBy或_.sortBy函数是否可以这样?
[
{
"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"
}
]
我尝试使用以下内容,但数组根本没有排序。
_.sortBy(self.messages(), 'priority');