如何按嵌套值对复杂对象进行排序

Posted

技术标签:

【中文标题】如何按嵌套值对复杂对象进行排序【英文标题】:How to sort a complex object by a nested value 【发布时间】:2019-07-02 22:44:05 【问题描述】:

我在尝试对复杂对象进行排序时遇到问题。这是对象结构:

[
    "searchResultProperties": [
        "key": "message_time",
        "value": 1542088800000
    , 
        "key": "size_byte AVG",
        "value": 480
    , 
        "key": "source_file",
        "value": "log"
    , 
        "key": "source_host",
        "value": "lab8.domain.com"
    ],
    "show": false,
    "key": null,
    "type": null
, 
    "searchResultProperties": [
        "key": "message_time",
        "value": 1542096000000
    , 
        "key": "size_byte AVG",
        "value": 373
    , 
        "key": "source_file",
        "value": "log"
    , 
        "key": "source_host",
        "value": "lab8.domain.com"
    ],
    "show": false,
    "key": null,
    "type": null
, 
    "searchResultProperties": [
        "key": "message_time",
        "value": 1542103200000
    , 
        "key": "size_byte AVG",
        "value": 683
    , 
        "key": "source_file",
        "value": "log"
    , 
        "key": "source_host",
        "value": "lab8.domain.com"
    ],
    "show": false,
    "key": null,
    "type": null
]

基本上它是一个对象数组,具有一个名为“searchResultProperties”的属性,该属性是一个具有“key”和“value”属性的对象数组。

我正在尝试对“key”属性进行排序,即:“size_byte AVG”。

【问题讨论】:

您能否也发布您的尝试? 不清楚。您是想按其中一个属性对数组中的对象进行排序,还是想对对象的属性进行排序,这似乎很奇怪? 我想根据 tue 属性之一对数组中的对象进行排序。 【参考方案1】:

您可以使用Array.prototype.sort() 函数为您提供两个对象,并且您可以根据您的排序逻辑返回一个+ve 或-ve 值。参考[https://developer.mozilla.org/en-US/docs/Web/javascript/Reference/Global_Objects/Array/sort]

array = [
    "searchResultProperties": [
        "key": "message_time",
        "value": 1542088800000
    , 
        "key": "size_byte AVG",
        "value": 480
    , 
        "key": "source_file",
        "value": "log"
    , 
        "key": "source_host",
        "value": "lab8.domain.com"
    ],
    "show": false,
    "key": null,
    "type": null
, 
    "searchResultProperties": [
        "key": "message_time",
        "value": 1542096000000
    , 
        "key": "size_byte AVG",
        "value": 373
    , 
        "key": "source_file",
        "value": "log"
    , 
        "key": "source_host",
        "value": "lab8.domain.com"
    ],
    "show": false,
    "key": null,
    "type": null
, 
    "searchResultProperties": [
        "key": "message_time",
        "value": 1542103200000
    , 
        "key": "size_byte AVG",
        "value": 683
    , 
        "key": "source_file",
        "value": "log"
    , 
        "key": "source_host",
        "value": "lab8.domain.com"
    ],
    "show": false,
    "key": null,
    "type": null
]

array = array.sort((a,b) => a.searchResultProperties.find(obj => obj.key === "size_byte AVG").value - b.searchResultProperties.find(obj => obj.key === "size_byte AVG").value )

console.log(array);

【讨论】:

我正在写一个答案,同时想快速回复,以便作者自己尝试 仍然没有解释为什么你的代码可以帮助 OP 以及 OP 做错了什么 @CalvinNunes OP 没有分享他的代码。所以我永远不能说他做错了什么! 代码有帮助,因为它可以工作并且可以排序!它是一条线的解决方案!当代码自己说话时,你为什么要指望我写一篇文章! 好的,现在你有一个解释你的代码做什么的文本,并帮助 OP 理解你的代码的使用。【参考方案2】:

您可以使用sortfind

sort 接受两个值作为参数。通过 find 我们找到键为 'size_byte AVG' 的数组元素并从中取出值并比较值

let arr = ["searchResultProperties": ["key": "message_time","value": 1542088800000, "key": "size_byte AVG","value": 480, "key": "source_file",       "value": "log", "key": "source_host","value": "lab8.domain.com"],"show": false,"key": null, "type": null,  "searchResultProperties": ["key": "message_time","value": 1542096000000, "key": "size_byte AVG","value": 373, "key": "source_file","value": "log", "key": "source_host","value": "lab8.domain.com"],"show": false,"key": null, "type": null, "searchResultProperties": ["key": "message_time","value": 1542103200000, "key": "size_byte AVG","value": 683 ,  "key": "source_file","value": "log", "key": "source_host","value": "lab8.domain.com"],"show": false,"key": null,"type": null]

let op = arr.sort((a,b)=>
         a.searchResultProperties.find(e=>e.key==='size_byte AVG').value -
         b.searchResultProperties.find(e=>e.key==='size_byte AVG').value)

console.log(op)

【讨论】:

谢谢!但这个不适用于“source_file”字段,因为它不是 int。 @Pennywise83 现在可以根据需要轻松修改,不是吗?顺便说一句,如果您希望两者都适用,可以使用Intl.collator【参考方案3】:

您可以使用函数find 查找键为size_byte AVG 的对象,并使用函数sort 对数组进行排序。

这种方法按升序排序

let arr = [    "searchResultProperties": [        "key": "message_time",        "value": 1542088800000    ,         "key": "size_byte AVG",        "value": 480    ,         "key": "source_file",        "value": "log"    ,         "key": "source_host",        "value": "lab8.domain.com"    ],    "show": false,    "key": null,    "type": null,     "searchResultProperties": [        "key": "message_time",        "value": 1542096000000    ,         "key": "size_byte AVG",        "value": 373    ,         "key": "source_file",        "value": "log"    ,         "key": "source_host",        "value": "lab8.domain.com"    ],    "show": false,    "key": null,    "type": null,     "searchResultProperties": [        "key": "message_time",        "value": 1542103200000    ,         "key": "size_byte AVG",        "value": 683    ,         "key": "source_file",        "value": "log"    ,         "key": "source_host",        "value": "lab8.domain.com"    ],    "show": false,    "key": null,    "type": null],
    target = "size_byte AVG",
    compare = (a, b) => a.find((key) => key === target).value - b.find((key) => key === target).value,
    sorted = arr.sort((searchResultProperties: a, searchResultProperties: b) => compare(a, b));

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

【讨论】:

以上是关于如何按嵌套值对复杂对象进行排序的主要内容,如果未能解决你的问题,请参考以下文章

如何按对象中的值对数组进行排序

Android-java-如何按对象内的某个值对对象列表进行排序

按属性值对对象列表进行排序[重复]

将嵌套字典按内部值排序

在Java中按属性值对对象ArrayList进行排序

根据嵌套键值对对象数组进行排序的最快方法