Skip to content

Instantly share code, notes, and snippets.

@ErickCodigo
Created November 5, 2020 19:46
Show Gist options
  • Save ErickCodigo/f162bf4b654a9a8204e4c60e35b3a806 to your computer and use it in GitHub Desktop.
Save ErickCodigo/f162bf4b654a9a8204e4c60e35b3a806 to your computer and use it in GitHub Desktop.
const sortIterable = prop => xs => {
let lon = xs.length;
let arr = [...xs];
while(lon > 1) {
lon--;
for(let o = 0; o < lon; o++) {
if(arr[o][prop] > arr[o+1][prop]) {
let temp = arr[o];
arr[o] = arr[o+1];
arr[o+1] = temp;
}
}
}
return [...arr];
}
@ErickCodigo
Copy link
Author

Sugerencia más corta:

const sortIterable = prop => xs => xs.sort((a, b) => a[prop] - b[prop])

@ErickCodigo
Copy link
Author

Solución con complejidad O(n) basado en indices numericos:
Necesita siempre tener indices numericos y que sean diferentes uno del otro

const sortIterableNumeric = prop => xs => { const solution = []; xs.forEach(o => solution[o[prop]] = o); return solution.filter(s => s); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment