Sort array of objects by single key with date value

后端 未结 19 1694
情话喂你
情话喂你 2020-11-22 10:56

I have an array of objects with several key value pairs, and I need to sort them based on \'updated_at\':

[
    {
        \"updated_at\" : \"2012-01-01T06:25         


        
19条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 11:36

    Data Imported

    [
        {
            "gameStatus": "1",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-20 11:32:04"
        },
        {
            "gameStatus": "0",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-19 18:08:24"
        },
        {
            "gameStatus": "2",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-19 18:35:40"
        },
        {
            "gameStatus": "0",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-19 10:42:53"
        },
        {
            "gameStatus": "2",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-20 10:54:09"
        },
        {
            "gameStatus": "0",
            "userId": "1a2fefb0-5ae2-47eb-82ff-d1b2cc27875a",
            "created_at": "2018-12-19 18:46:22"
        },
        {
            "gameStatus": "1",
            "userId": "7118ed61-d8d9-4098-a81b-484158806d21",
            "created_at": "2018-12-20 10:50:48"
        }
    ]
    

    FOR Ascending order

    arr.sort(function(a, b){
        var keyA = new Date(a.updated_at),
            keyB = new Date(b.updated_at);
        // Compare the 2 dates
        if(keyA < keyB) return -1;
        if(keyA > keyB) return 1;
        return 0;
    });
    

    Example for Asc Order

    [
        {
            "gameStatus": "0",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-19 10:42:53"
        },
        {
            "gameStatus": "0",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-19 18:08:24"
        },
        {
            "gameStatus": "2",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-19 18:35:40"
        },
        {
            "gameStatus": "0",
            "userId": "1a2fefb0-5ae2-47eb-82ff-d1b2cc27875a",
            "created_at": "2018-12-19 18:46:22"
        },
        {
            "gameStatus": "1",
            "userId": "7118ed61-d8d9-4098-a81b-484158806d21",
            "created_at": "2018-12-20 10:50:48"
        },
        {
            "gameStatus": "2",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-20 10:54:09"
        },
        {
            "gameStatus": "1",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-20 11:32:04"
        }
    ]
    

    FOR Descending order

    arr.sort(function(a, b){
        var keyA = new Date(a.updated_at),
            keyB = new Date(b.updated_at);
        // Compare the 2 dates
        if(keyA > keyB) return -1;
        if(keyA < keyB) return 1;
        return 0;
    });
    

    Example for Desc Order

    [
        {
            "gameStatus": "1",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-20 11:32:04"
        },
        {
            "gameStatus": "2",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-20 10:54:09"
        },
        {
            "gameStatus": "1",
            "userId": "7118ed61-d8d9-4098-a81b-484158806d21",
            "created_at": "2018-12-20 10:50:48"
        },
        {
            "gameStatus": "0",
            "userId": "1a2fefb0-5ae2-47eb-82ff-d1b2cc27875a",
            "created_at": "2018-12-19 18:46:22"
        },
        {
            "gameStatus": "2",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-19 18:35:40"
        },
        {
            "gameStatus": "0",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-19 18:08:24"
        },
        {
            "gameStatus": "0",
            "userId": "c02cfb18-ae66-430b-9524-67d9dd8f6a50",
            "created_at": "2018-12-19 10:42:53"
        }
    ]
    

提交回复
热议问题