简体   繁体   English

如何根据属性对对象数组进行排序

[英]How to sort Object array based on property

I have a requirement to sort a object array based on frozenEdge property.我需要根据frozenEdge 属性对对象数组进行排序。 If property value is start we should move item to top and property value is end we have to move item to bottom.如果属性值是开始,我们应该将项目移到顶部,属性值是结束,我们必须将项目移到底部。 Item which doesn't have field property are invalid so there position should remain as it ,In below array for example first item is invalid so we should not reorder.没有字段属性的项目无效,因此位置应保持原样,例如在下面的数组中,第一个项目无效,因此我们不应重新排序。

I have tried a sample program which work in firefox but failing in chrome I am looking for a solution which can work in chrome, safari and firefox我尝试了一个在 Firefox 中运行但在 chrome 中失败的示例程序我正在寻找一个可以在 chrome、safari 和 firefox 中运行的解决方案

Actual Array实际数组

[{
  "style": "width: 10px; text-align: center",
  "className": "oj-sm-only-hide table",
  "headerClassName": "Sample"
}, {
  "headerText": "Fee Description",
  "field": "Description"
}, {
  "headerText": "Amount",
  "field": "FeeAmount",
  "frozenEdge": "start"
}, {
  "headerText": "Currency",
  "field": "CurrencyCode",
  "frozenEdge": "start"
}, {
  "headerText": "Status",
  "field": "FeeStatus",
}, {
  "headerText": "Department",
  "field": "DepartmentDescription"
}, {
  "headerText": "Assessed Date",
  "field": "AssessedDate",
  "frozenEdge": "end"
}, {
  "headerText": "Payment Date",
  "field": "PaymentDate"
}, {
  "headerText": "Paid By",
  "field": "PaidBy",
  "frozenEdge": "end"
}, {
  "headerText": "Invoice",
  "field": "Invoice"
}, {
  "headerClassName": "table-header table-row-botton",
  "className": "table-row-botton"
}]

Expected Output预期产出

[{
  "style": "width: 10px; text-align: center",
  "className": "oj-sm-only-hide table",
  "headerClassName": "Sample"
}, {
  "headerText": "Amount",
  "field": "FeeAmount",
  "frozenEdge": "start"
}, {
  "headerText": "Currency",
  "field": "CurrencyCode",
  "frozenEdge": "start"
}, {
  "headerText": "Fee Description",
  "field": "Description"
}, {
  "headerText": "Status",
  "field": "FeeStatus",
}, {
  "headerText": "Department",
  "field": "DepartmentDescription"
}, {
  "headerText": "Payment Date",
  "field": "PaymentDate"
}, {
  "headerText": "Invoice",
  "field": "Invoice"
}, {
  "headerText": "Assessed Date",
  "field": "AssessedDate",
  "frozenEdge": "end"
}, {
  "headerText": "Paid By",
  "field": "PaidBy",
  "frozenEdge": "end"
}, {
  "headerClassName": "table-header table-row-botton",
  "className": "table-row-botton"
}]

Working code for Firefox: Firefox 的工作代码:

 function isValidColumn(column, ignoreFR) { var retVal = true; if (!column.hasOwnProperty('field')) { return false; } return retVal; } var array = [{ "style": "width: 10px; text-align: center", "className": "oj-sm-only-hide table", "headerClassName": "Sample" }, { "headerText": "Fee Description", "field": "Description" }, { "headerText": "Amount", "field": "FeeAmount", "frozenEdge": "start" }, { "headerText": "Currency", "field": "CurrencyCode", "frozenEdge": "start" }, { "headerText": "Status", "field": "FeeStatus", }, { "headerText": "Department", "field": "DepartmentDescription" }, { "headerText": "Assessed Date", "field": "AssessedDate", "frozenEdge": "end" }, { "headerText": "Payment Date", "field": "PaymentDate" }, { "headerText": "Paid By", "field": "PaidBy", "frozenEdge": "end" }, { "headerText": "Invoice", "field": "Invoice" }, { "headerClassName": "table-header table-row-botton", "className": "table-row-botton" }]; array.sort( function(col1, col2) { if (isValidColumn(col1, true) && isValidColumn(col2, true)) { if (col2.hasOwnProperty('frozenEdge') && col2.frozenEdge == 'start') { return (col2.frozenEdge === 'start' && col1.frozenEdge !== 'start') ? 1 : (col1.frozenEdge === 'start') ? -1 : 0; } else if (col1.hasOwnProperty('frozenEdge') && col1.frozenEdge == 'end') { return (col1.frozenEdge == 'end' && col2.frozenEdge !== 'end') ? 1 : (col1.frozenEdge === 'end') ? -1 : 0; } else { return 0; } } else { return 0; } } ); console.log(array);
 .as-console-wrapper { top: 0; max-height: 100% !important; }

You could take an object with the wanted order and check field first.您可以先获取具有所需订单和检查field的对象。

 const order = { start: -1, default: 0, end: 1 }, data = [{ style: "width: 10px; text-align: center", className: "oj-sm-only-hide table", headerClassName: "Sample" }, { headerText: "Fee Description", field: "Description" }, { headerText: "Amount", field: "FeeAmount", frozenEdge: "start" }, { headerText: "Currency", field: "CurrencyCode", frozenEdge: "start" }, { headerText: "Status", field: "FeeStatus" }, { headerText: "Department", field: "DepartmentDescription" }, { headerText: "Assessed Date", field: "AssessedDate", frozenEdge: "end" }, { headerText: "Payment Date", field: "PaymentDate" }, { headerText: "Paid By", field: "PaidBy", frozenEdge: "end" }, { headerText: "Invoice", field: "Invoice" }, { headerClassName: "table-header table-row-botton", className: "table-row-botton" }]; data.sort((a, b) => a.field && b.field && (order[a.frozenEdge] || order.default) - (order[b.frozenEdge] || order.default) ); console.log(data);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

This should work, if you want items with frozenEdge: "start" on top and frozenEdge: "end" on the bottom:如果您想要带有frozenEdge: "start"在顶部和frozenEdge: "end"在底部的项目,这应该可以工作:

array.sort((x, y) => {
    if (y.frozenEdge === undefined && x.frozenEdge == undefined) return 0

    if (y.frozenEdge === undefined && x.frozenEdge == "end") return 1
    if (y.frozenEdge === undefined && x.frozenEdge == "start") return -1

    if (y.frozenEdge == "end" && x.frozenEdge == undefined) return -1
    if (y.frozenEdge == "start" && x.frozenEdge == undefined ) return 1
})

Checkout mdn docs on Array.prototype.sort() for more info.查看Array.prototype.sort()上的 mdn 文档了解更多信息。

It seems that in your "Expected Output" snippet you actually wanted to keep the first and last elements of the array at their indices before sorting.似乎在您的“预期输出”片段中,您实际上希望在排序之前将数组的第一个和最后一个元素保留在它们的索引处。 I'd suggest to pop() and shift() the last and first items respectively, something like this:我建议分别pop()shift()最后一项和第一项,如下所示:

const first = array.shift()
const last = array.pop()

array = array.sort( /*...*/ )

array = [first, ...array, last]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM