¿Cómo puedo ordenar esta matriz en función de la identificación?
const arr = [{ "id": 38938888, "subInternalUpdates": true, }, { "id": 38938887, "subInternalUpdates": true }, { "id": 38938889, "subInternalUpdates": true } ]; const sorted_by_name = arr.sort((a, b) => a.id > b.id); console.log(sorted_by_name);Rendimiento esperado
const arr = [ { "id": 38938887, "subInternalUpdates": true }, { "id": 38938888, "subInternalUpdates": true, }, { "id": 38938889, "subInternalUpdates": true } ];Puede comparar directamente usando ab ; de lo contrario, si está comparando los valores, debe devolver -1, 0 o 1 para que la ordenación funcione correctamente
const arr = [{ "id": 38938888, "subInternalUpdates": true, }, { "id": 38938887, "subInternalUpdates": true }, { "id": 38938889, "subInternalUpdates": true } ]; const sorted_by_name = arr.sort((a, b) => a.id - b.id); console.log(sorted_by_name);Mucho mejor devuelva a.id - b.id cuando ordene la matriz:
const arr = [{ "id": 38938888, "subInternalUpdates": true, }, { "id": 38938887, "subInternalUpdates": true }, { "id": 38938889, "subInternalUpdates": true } ]; const sorted_by_name = arr.sort((a, b) => { return a.id - b.id; }); console.log(sorted_by_name);