This function returns an array with the same length as the input array, where each element is the rank of the corresponding element in the input array.
JavaScript:
let rankArray = arr => arr.slice().sort((a, b) => a - b).map((v, i, sortedArr) => sortedArr.indexOf(arr[i]) + 1);
// Example
console.log(rankArray([40, 20, 10, 30])); // [4, 2, 1, 3]
console.log(rankArray([1.2, 0.5, 1.1, 0.9])); // [4, 1, 3, 2]
TypeScript:
let rankArray = (arr: number[]): number[] => arr.slice().sort((a, b) => a - b).map((v, i, sortedArr) => sortedArr.indexOf(arr[i]) + 1);
// Example
console.log(rankArray([40, 20, 10, 30])); // [4, 2, 1, 3]
console.log(rankArray([1.2, 0.5, 1.1, 0.9])); // [4, 1, 3, 2]