Tengo una matriz de servicios de locationServices que contiene una matriz de ubicaciones y una matriz de servicios anidados en el estado.
Mi funcionalidad a continuación consiste en crear una nueva matriz, calcular y agregar algunas propiedades adicionales a una instancia de locationService , empujar a la matriz respectiva y luego ordenar la matriz respectiva.
let locationsWithDistance = [] let locationsWithNeedMatch = [] let locationsWithDistanceAndNeedMatch = [] locationServices?.map((location) => { location['distance'] = calcLocationDistance(location) location['matchesNeed'] = calcNeedMatches(location) locationsWithDistance.push(location) locationsWithNeedMatch.push(location) locationsWithDistanceAndNeedMatch.push(location) locationsWithDistance.sort(function (a, b) { return a.distance - b.distance; }); locationsWithNeedMatch.sort(function (a, b) { return b.matchesNeed - a.matchesNeed; }); locationsWithDistanceAndNeedMatch.sort(function(a, b){ // need to sort by both distance and matches }) })la primera matriz ordena por distancia, la segunda ordena por coincidencia de necesidad. Para mi tercera matriz, me gustaría ordenar tanto por distancia como por coincidencia. ¿Cómo puedo conseguir esto?
SALIDA DISEÑADA
[ { location_id: 1, name: 'location 1', matchesNeed: true, distance: 4.7 }, { location_id: 3, name: 'location 3', matchesNeed: true, distance: 9.9 }, { location_id: 4, name: 'location 4', matchesNeed: false, distance: 5.4 }, { location_id: 5, name: 'location 5', matchesNeed: false, distance: 5.8 }, { location_id: 6, name: 'location 6', matchesNeed: false, distance: 8.7 }, ]la matriz devuelve primero las coincidencias de necesidad si son verdaderas, y la que esté más cerca del usuario, y luego, después de que el resto de las necesidades se devuelvan por distancia más cercana con la necesidad del usuario de false