Sorting array of objects in Javascript

74 Views Asked by At

I want to sort the data on the basis of the property activeSince but it is not sorted as expected. Here is the code that is being used to sort the data and the data is given as well.

What are the possible ways to do that and is it even possible to sort such type of data structure ?

function sortChatsByActiveSince(filteredData) {
  filteredData.sort((a, b) => {
      if (a.chats.length === 0 && b.chats.length === 0) return 0;
      if (a.chats.length === 0) return  1;
      if (b.chats.length === 0) return -1;

      if (this.sortOrder === "asc") {
        return a.chats[0].activeSince - b.chats[0].activeSince;
      } else {
        return b.chats[0].activeSince - a.chats[0].activeSince;
      }
    });
    for (let data of filteredData) {
      data.chats.sort((a, b) => {
        if (this.sortOrder === "asc") {
          return a.activeSince - b.activeSince;
        } else {
          return b.activeSince - a.activeSince;
        }
      });
    }
    console.log(JSON.stringify(filteredData, null, 2))
}




const filteredData = [
  {
    id: "64e32c286fc5967676d558bc",
    name: "queue",
    chats: [
      {
        name: "jenny",
        identity: "113378945445",
        mediaType: "WEB",
        activeSince: 500,
      },
    ],
  },
  {
    id: "64e32c146fc5967676d558b2",
    name: "chat 2 queue",
    chats: [
      {
        name: "jenny",
        identity: "113378789",
        mediaType: "WEB",
        activeSince: 900,
      },
    ],
  },
  {
    id: "64d4d5fe1d7dd34b92f0a7f0",
    name: "chat 1 queue",
    chats: [
      {
        name: "jenny",
        identity: "113311",
        mediaType: "WEB",
        activeSince: 400,
      },
    ],
  },
  {
    id: "64c90af1eb1d14361be733cf",
    name: "Web Queue",
    chats: [
      {
        name: "jenny",
        identity: "113345624",
        mediaType: "WEB",
        activeSince: 1424,
        timestamp: "2023-08-21T10:58:50.070+00:00",
        agentName: "junaid",
      },
      {
        name: "jenny",
        identity: "11331",
        mediaType: "WEB",
        activeSince: 5396,
      },
    ],
  },
  {
    id: "64d4dab11d7dd34b92f0a90b",
    name: "chat 3 queue",
    chats: [
      {
        name: "jenny",
        identity: "113378978",
        mediaType: "WEB",
        activeSince: 4644,
        timestamp: "2023-08-21T10:05:10.127+00:00",
      },
      {
        name: "jenny",
        identity: "113378978",
        mediaType: "WEB",
        activeSince: 300,
        timestamp: "2023-08-21T10:05:10.127+00:00",
      },
      {
        name: "jenny",
        identity: "113378978",
        mediaType: "WEB",
        activeSince: 1400,
        timestamp: "2023-08-21T10:05:10.127+00:00",
      },
      {
        name: "jenny",
        identity: "113378978",
        mediaType: "WEB",
        activeSince: 325,
        timestamp: "2023-08-21T10:05:10.127+00:00",
      },
    ],
  },
];

sortChatsByActiveSince(filteredData);

I want to sort the whole data on the basis of the property activeSince and with this implementation, the nested array is being sorted of each object but not the whole object. Here is the outPut of the above implementation.

  {
    "id": "64d4dab11d7dd34b92f0a90b",
    "name": "chat 3 queue",
    "chats": [
      {
        "name": "jenny",
        "identity": "113378978",
        "mediaType": "WEB",
        "activeSince": 4644,
        "timestamp": "2023-08-21T10:05:10.127+00:00"
      },
      {
        "name": "jenny",
        "identity": "113378978",
        "mediaType": "WEB",
        "activeSince": 1400,
        "timestamp": "2023-08-21T10:05:10.127+00:00"
      },
      {
        "name": "jenny",
        "identity": "113378978",
        "mediaType": "WEB",
        "activeSince": 325,
        "timestamp": "2023-08-21T10:05:10.127+00:00"
      },
      {
        "name": "jenny",
        "identity": "113378978",
        "mediaType": "WEB",
        "activeSince": 300,
        "timestamp": "2023-08-21T10:05:10.127+00:00"
      }
    ]
  },
  {
    "id": "64c90af1eb1d14361be733cf",
    "name": "Web Queue",
    "chats": [
      {
        "name": "jenny",
        "identity": "11331",
        "mediaType": "WEB",
        "activeSince": 5396
      },
      {
        "name": "jenny",
        "identity": "113345624",
        "mediaType": "WEB",
        "activeSince": 1424,
        "timestamp": "2023-08-21T10:58:50.070+00:00",
        "agentName": "junaid"
      }
    ]
  },
  {
    "id": "64e32c146fc5967676d558b2",
    "name": "chat 2 queue",
    "chats": [
      {
        "name": "jenny",
        "identity": "113378789",
        "mediaType": "WEB",
        "activeSince": 900
      }
    ]
  },
  {
    "id": "64e32c286fc5967676d558bc",
    "name": "queue",
    "chats": [
      {
        "name": "jenny",
        "identity": "113378945445",
        "mediaType": "WEB",
        "activeSince": 500
      }
    ]
  },
  {
    "id": "64d4d5fe1d7dd34b92f0a7f0",
    "name": "chat 1 queue",
    "chats": [
      {
        "name": "jenny",
        "identity": "113311",
        "mediaType": "WEB",
        "activeSince": 400
      }
    ]
  }
]
1

There are 1 best solutions below

2
btilly On

The following seems to work as expected. The sortOrder is not "asc" so it sorts descending. Both within chats. And by conversation.

function sortChatsByActiveSince(filteredData) {
  let isAsc = (this.sortOrder === "asc");

  for (let data of filteredData) {
    data.chats.sort((a, b) => {
      if (isAsc) {
        return a.activeSince - b.activeSince;
      } else {
        return b.activeSince - a.activeSince;
      }
    });
  }
  filteredData.sort((a, b) => {
      let response;
      if (a.chats.length === 0 && b.chats.length === 0) {
        response = 0;
      } else if (a.chats.length === 0) {
        response = 1;
      } else if (b.chats.length === 0) {
        response = -1;
      } else {
        response = a.chats[0].activeSince - b.chats[0].activeSince;
      }

      if (isAsc) {
        return response;
      } else {
        return -response;
      }
    });
    console.log(JSON.stringify(filteredData, null, 2))
}




const filteredData = [
  {
    id: "64e32c286fc5967676d558bc",
    name: "queue",
    chats: [
      {
        name: "jenny",
        identity: "113378945445",
        mediaType: "WEB",
        activeSince: 500,
      },
    ],
  },
  {
    id: "64e32c146fc5967676d558b2",
    name: "chat 2 queue",
    chats: [
      {
        name: "jenny",
        identity: "113378789",
        mediaType: "WEB",
        activeSince: 900,
      },
    ],
  },
  {
    id: "64d4d5fe1d7dd34b92f0a7f0",
    name: "chat 1 queue",
    chats: [
      {
        name: "jenny",
        identity: "113311",
        mediaType: "WEB",
        activeSince: 400,
      },
    ],
  },
  {
    id: "64c90af1eb1d14361be733cf",
    name: "Web Queue",
    chats: [
      {
        name: "jenny",
        identity: "113345624",
        mediaType: "WEB",
        activeSince: 1424,
        timestamp: "2023-08-21T10:58:50.070+00:00",
        agentName: "junaid",
      },
      {
        name: "jenny",
        identity: "11331",
        mediaType: "WEB",
        activeSince: 5396,
      },
    ],
  },
  {
    id: "64d4dab11d7dd34b92f0a90b",
    name: "chat 3 queue",
    chats: [
      {
        name: "jenny",
        identity: "113378978",
        mediaType: "WEB",
        activeSince: 4644,
        timestamp: "2023-08-21T10:05:10.127+00:00",
      },
      {
        name: "jenny",
        identity: "113378978",
        mediaType: "WEB",
        activeSince: 300,
        timestamp: "2023-08-21T10:05:10.127+00:00",
      },
      {
        name: "jenny",
        identity: "113378978",
        mediaType: "WEB",
        activeSince: 1400,
        timestamp: "2023-08-21T10:05:10.127+00:00",
      },
      {
        name: "jenny",
        identity: "113378978",
        mediaType: "WEB",
        activeSince: 325,
        timestamp: "2023-08-21T10:05:10.127+00:00",
      },
    ],
  },
];

sortChatsByActiveSince(filteredData);