ts order array code example
Example 1: javascript sort object by key
function sortObjectByKeys(o) {
return Object.keys(o).sort().reduce((r, k) => (r[k] = o[k], r), {});
}
var unsorted = {"c":"crane","b":"boy","a":"ant"};
var sorted=sortObjectByKeys(unsorted);
Example 2: how to sort numbers in typescript
var numericArray: number[] = [2, 3, 4, 1, 5, 8, 11];
var sortedArray: number[] = numericArray.sort((n1,n2) => n1 - n2);
Example 3: how to sort numbers in typescript
var stringArray: string[] = ['AB', 'Z', 'A', 'AC'];
var sortedArray: string[] = stringArray.sort((n1,n2) => {
if (n1 > n2) {
return 1;
}
if (n1 < n2) {
return -1;
}
return 0;
});