Psychologist
Psychologist

Reputation: 21

Sort by object property in two dimensional array in Javascript/Node.Js

I am analysing log files and I'd like to make it easier to read by sorting the requests by descending order. The problem is that I have a two dimensional array with an object in each, I want to sort the arrays by one of their object property (Average response time here) and I don't know how to access to it...

Here is a piece of the final log file for you to understand:

[
    [
        {
            "Request": "/sql/sqlweb/",
            "Average response time": "2.685 ms",
            "Number of calls": 1
        }
    ],
    [
        {
            "Request": "/",
            "Average response time": "1.219 ms",
            "Number of calls": 2529
        }
    ],
    [
        {
            "Request": "/mysql/admin/",
            "Average response time": "4.086 ms",
            "Number of calls": 1
        }
    ],
    [
        {
            "Request": "/mysql/sqlmanager/",
            "Average response time": "2.774 ms",
            "Number of calls": 1
        }
    ],
    [
        {
            "Request": "/phpmyadmin/",
            "Average response time": "2.417 ms",
            "Number of calls": 2
        }
    ]
]

This is after a JSON.stringify() on my array. I can also copy/paste the current version of my code if you need it.

Ps: I can easily remove the "ms" if it helps.

Upvotes: 2

Views: 276

Answers (4)

amtg
amtg

Reputation: 124

You need to use array.sort with a custom comparator.

Upvotes: 0

JakeBoggs
JakeBoggs

Reputation: 274

You can do this:

[
    [
        {
            "Request": "/sql/sqlweb/",
            "Average response time": "2.685 ms",
            "Number of calls": 1
        }
    ],
    [
        {
            "Request": "/",
            "Average response time": "1.219 ms",
            "Number of calls": 2529
        }
    ],
    [
        {
            "Request": "/mysql/admin/",
            "Average response time": "4.086 ms",
            "Number of calls": 1
        }
    ],
    [
        {
            "Request": "/mysql/sqlmanager/",
            "Average response time": "2.774 ms",
            "Number of calls": 1
        }
    ],
    [
        {
            "Request": "/phpmyadmin/",
            "Average response time": "2.417 ms",
            "Number of calls": 2
        }
    ]
].sort(function(a, b) {
    return Number(a[0]["Average response time"].replace(/[^0-9]+/g, "")) - Number(b[0]["Average response time"].replace(/[^0-9]+/g, ""));
});

Upvotes: 0

Dij
Dij

Reputation: 9808

you can use sort(), you can write a comparator function in which you compare objects inside array using there average response times, for this you can use parseFloat()

var arr = [
[
    {
        "Request": "/sql/sqlweb/",
        "Average response time": "2.685 ms",
        "Number of calls": 1
    }
],
[
    {
        "Request": "/",
        "Average response time": "1.219 ms",
        "Number of calls": 2529
    }
],
[
    {
        "Request": "/mysql/admin/",
        "Average response time": "4.086 ms",
        "Number of calls": 1
    }
],
[
    {
        "Request": "/mysql/sqlmanager/",
        "Average response time": "2.774 ms",
        "Number of calls": 1
    }
],
[
    {
        "Request": "/phpmyadmin/",
        "Average response time": "2.417 ms",
        "Number of calls": 2
    }
]
];

arr.sort(function(a,b){
return parseFloat(a[0]["Average response time"]) - parseFloat(b[0]["Average response time"]);
});

console.log(arr);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386624

You could take the first object of the inner array and then the wanted property for sorting.

var array = [[{ Request: "/sql/sqlweb/", "Average response time": "2.685 ms", "Number of calls": 1 }], [{ Request: "/", "Average response time": "1.219 ms", "Number of calls": 2529 }], [{ Request: "/mysql/admin/", "Average response time": "4.086 ms", "Number of calls": 1 }], [{ Request: "/mysql/sqlmanager/", "Average response time": "2.774 ms", "Number of calls": 1 }], [{ Request: "/phpmyadmin/", "Average response time": "2.417 ms", "Number of calls": 2 }]];

array.sort(function (a, b) {
    function getV(o) {
        return o[0]['Average response time'].match(/\d+\.?\d*/);
    }
    return getV(b) - getV(a);
});

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

Upvotes: 0

Related Questions