2

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.

1
  • you want to sort the inner arrays? so result will be similar to this (2 dimensional array) but sorted the inner array. or you want one final array (1 dimensional) where the objects will be there in sorted order? Commented Jul 24, 2017 at 15:52

4 Answers 4

1

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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; }

1 Comment

Actually I eventually found the answer to my question by adding the [0] index but thanks for your quick answer anyway! Ps: Thanks for the tip to keep only the value with the regex :)
0

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, ""));
});

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.