tengo esta matriz
let arr = [ { "time": "3", "wholeObj": "abc mo sa 3 Y", "mapEventValue": "Y" }, { "time": "3", "wholeObj": "abc a 3 G", "mapEventValue": "G" }, { "time": "3", "wholeObj": "cba d 3 S f", "mapEventValue": "S" }, { "time": "3", "wholeObj": "cba z 3 R", "mapEventValue": "R" } ] Necesito ordenar la matriz PRIMERO según el valor de time y luego, si los valores de tiempo son iguales, necesito ordenarlos según mapEventValue en el siguiente orden GYRS
Entonces, en mi caso, todos los objetos tienen valores de tiempo iguales 3 No puedo encontrar una manera de ordenarlos por la propiedad mapEventValue
lo que intenté
Hasta ahora solo logré ordenarlos por valor de tiempo
let sorted = arr.sort((a,b) => a.time - b.time)
Así que mi salida al final debería ser
let arr = [ { "time": "3", "wholeObj": "abc a 3 G", "mapEventValue": "G" }, { "time": "3", "wholeObj": "abc mo sa 3 Y", "mapEventValue": "Y" }, { "time": "3", "wholeObj": "cba z 3 R", "mapEventValue": "R" }, { "time": "3", "wholeObj": "cba d 3 S f", "mapEventValue": "S" }, ]Por lo tanto, solo ordene por time si el time difiere, de lo contrario, ordene por el índice de su orden de clasificación deseado.
let sortOrder = 'GYRS'; let arr = [ { "time": "3", "wholeObj": "abc mo sa 3 Y", "mapEventValue": "Y" }, { "time": "3", "wholeObj": "abc a 3 G", "mapEventValue": "G" }, { "time": "3", "wholeObj": "cba d 3 S f", "mapEventValue": "S" }, { "time": "3", "wholeObj": "cba z 3 R", "mapEventValue": "R" } ]; let sorted = arr.sort((a,b) => { if (a.time != b.time) { return a.time - b.time; } else { return sortOrder.indexOf(a.mapEventValue) - sortOrder.indexOf(b.mapEventValue); } }); console.log(sorted);