Search code examples
javascriptecmascript-6reduxnormalizr

Sort after using Normalizr


After using Normalizr I have an array like this:

comments : {
        byId : {
            "comment1" : {
                id : "comment1",
                author : "user2",
                date: "2017-05-09 05:30:00",
                comment : "....."
            },
            "comment2" : {
                id : "comment2",
                author : "user3",
                date: "2017-04-19 04:30:00",
                comment : "....."
            },
            "comment3" : {
                id : "comment3",
                author : "user3",
                date: "2017-05-19 05:40:00",
                comment : "....."
            },
            "comment4" : {
                id : "comment4",
                author : "user1",
                date: "2017-08-06 05:30:00",
                comment : "....."
            },
            "comment5" : {
                id : "comment5",
                author : "user3",
                date: "2017-07-01 07:30:00",
                comment : "....."
            },
        },
        allIds : ["comment1", "comment2", "comment3", "commment4", "comment5"]
    },

Now I have a button to change order between id or date. Then I need to change allIds (that retains sort order) to sort by date. The allIds should look like this:

allIds : ["comment2", "comment1", "comment3", "commment5", "comment4"] // sort by date

I do not know how this order could be made. I have made several unsuccessful attempts with javascript sort.


Solution

  • You can simply iterate the objects using Object.keys() and then sort sort it by the property date (parsed as Date):

    var comments = {
      byId: {
        "comment1": {
          id: "comment1",
          author: "user2",
          date: "2017-05-09 05:30:00",
          comment: ".....",
        },
        "comment2": {
          id: "comment2",
          author: "user3",
          date: "2017-04-19 04:30:00",
          comment: ".....",
        },
        "comment6": {
          id: "comment6",
          author: "user3",
          date: "2017-07-01 07:30:00",
          comment: ".....485",
        },
        "comment3": {
          id: "comment3",
          author: "user3",
          date: "2017-05-19 05:40:00",
          comment: ".....",
        },
        "comment4": {
          id: "comment4",
          author: "user1",
          date: "2017-08-06 05:30:00",
          comment: ".....",
        },
        "comment5": {
          id: "comment5",
          author: "user3",
          date: "2017-07-01 07:30:00",
          comment: ".....",
        },
      },
      allIds: ["comment1", "comment2", "comment3", "commment4", "comment5"]
    };
    
    var results = Object.keys(comments.byId).sort((s, a) => {
      const date1 = Date.parse(comments.byId[s].date);
      const date2 = Date.parse(comments.byId[a].date);
      
      if (date1 === date2) {
        return s.localeCompare(a);
      }
      
      return date1 - date2;
    });
    
    console.log(results);

    References:


    Note: You forgot the commas after the date string. The commas after the comment string are not necessary.

    Update Added another sort condition.