Tengo una lista de tareas y cada tarea contiene la propiedad 'fromPriority' . Esta propiedad es una cadena y sus valores solo pueden ser 'HIGH' , 'AVERAGE' , 'LOW' o una cadena numérica. Necesito ordenar esta lista por esta propiedad, pero necesito que las cadenas numéricas se ordenen primero en orden descendente. Por ejemplo, si tengo los siguientes valores:
['1', '99', '10', 'HIGH', '5', 'LOW']quiero que el orden sea
['99', '10', '5', '1', 'HIGH', 'LOW']Esta puede ser una posible solución para lograr el objetivo deseado.
Fragmento de código
const myDict = { // helper object to assign numeric values for High, Average, Low 'HIGH': 2, 'AVERAGE': 1, 'LOW': 0 }; const customSort = arr => ( [...arr].sort((a, b) => ( // shallow-copy 'arr' and use '.sort()' a in myDict && b in myDict // if both a & b are High, Average or Low ? myDict[b] - myDict[a] // find difference in their costs (using 'myDict') : b in myDict // else, if b is either High, Average or Low ? -1 // then, keep b below a : a in myDict // else, if a is either High, Average or Low ? 1 // then, keep a below b : +b - +a // else, if both a & b are numeric-strings )) // convert to number (using '+') and order based on difference ); const rawData = ['1', '99', '10', 'HIGH', '5', 'LOW']; console.log(`[${rawData.join(', ')}]`, customSort(rawData)); console.log( "['15', 'AVERAGE', '20', 'HIGH', '52', 'LOW']", customSort(['15', 'AVERAGE', '20', 'HIGH', '52', 'LOW']) ); console.log( "['31', '49', '17', 'AVERAGE', '25', 'LOW']", customSort(['31', '49', '17', 'AVERAGE', '25', 'LOW']) ); console.log( "['41', 'HIGH', '90', 'LOW', '45', 'AVERAGE']", customSort(['41', 'HIGH', '90', 'LOW', '45', 'AVERAGE']) ); console.log( "['71', '32', '05', 'HIGH', '75', 'LOW']", customSort(['71', '32', '05', 'HIGH', '75', 'LOW']) );Explicación
Los comentarios en línea en el fragmento de código anterior proporcionan una descripción relevante de los pasos.
Este código implementa una función de clasificación personalizada para clasificar según la condición que devuelve 1/-1 significa que a es mayor/menor que b respectivamente.
let arr = ['99', '100', '2'] const custom_sort = (a, b) => { const numericA = !isNaN(a); const numericB = !isNaN(b); if (numericA && !numericB) return -1; if (!numericA && numericB) return 1; if (numericA && numericB) { if (parseInt(a) > parseInt(b)) return -1; return 1; } else { if(a==='LOW')return 1; if(a==='MED'&&b==='HIGH')return 1; return -1; } } console.log(arr.sort(custom_sort));Aquí hay una solución simple
function customSort(arr) { //first sort only the numbers let newArr = arr .filter(el => !isNaN(el)) //filter numbers .sort((a, b) => parseInt(b) - parseInt(a)) //sort numbers //now the alplabical part let str = ['HIGH', 'AVERAGE', 'LOW'] //arrange all possible strings str.forEach(el => (arr.includes(el) ? newArr.push(el) : null)) //push matched items in the newArr return newArr } let input = ['1', '99', '10', 'HIGH', '5', 'LOW'] let output = customSort(input) console.log(output)