typescript sort

Solutions on MaxInterview for typescript sort by the best coders in the world

showing results for - "typescript sort"
Guadalupe
09 Jan 2019
1// Numbers
2// When sorting numbers, you can use the compact comparison:
3
4var numericArray: number[] = [2, 3, 4, 1, 5, 8, 11];
5
6var sortedArray: number[] = numericArray.sort((n1,n2) => n1 - n2);
7// i.e. - rather than <.
8
9// Other Types
10// If you are comparing anything else, you'll need to convert the comparison into a number.
11
12var stringArray: string[] = ['AB', 'Z', 'A', 'AC'];
13
14var sortedArray: string[] = stringArray.sort((n1,n2) => {
15    if (n1 > n2) {
16        return 1;
17    }
18
19    if (n1 < n2) {
20        return -1;
21    }
22
23    return 0;
24});
25// Objects
26// For objects, you can sort based on a property, bear in mind the above information about being able to short-hand number types. The below example works irrespective of the type.
27
28var objectArray: { age: number; }[] = [{ age: 10}, { age: 1 }, {age: 5}];
29
30var sortedArray: { age: number; }[] = objectArray.sort((n1,n2) => {
31    if (n1.age > n2.age) {
32        return 1;
33    }
34
35    if (n1.age < n2.age) {
36        return -1;
37    }
38
39    return 0;
40});
Laura
01 Feb 2016
1let arr:{key:number}[] = [{key : 2}, {key : 3}, {key : 4}, {key : 1}, {key : 5}, {key : 8}, {key : 11}];
2
3let sortFn2 = (obj1 , obj2) => {key:number} { return obj1.key - obj2.key; }
4
5const sortedArray2:{key:number}[] = arr.sort(sortFn2);
6
Maelia
08 May 2017
1var numericArray: number[] = [2, 3, 4, 1, 5, 8, 11];
2
3var sortedArray: number[] = numericArray.sort((n1,n2) => n1 - n2);
Luna
14 Oct 2019
1var stringArray: string[] = ['AB', 'Z', 'A', 'AC'];
2
3var sortedArray: string[] = stringArray.sort((n1,n2) => {
4    if (n1 > n2) {
5        return 1;
6    }
7
8    if (n1 < n2) {
9        return -1;
10    }
11
12    return 0;
13});
queries leading to this page
typescript order by arrayarray sort in typescripttypescript sort array of numberangular 9 alphanumeric sort typescript for arraylistarray sort typescriptsort array by typescripttypescript sort array by numbertypescript alphabetical sorttypescript arraysortsort number array typetypescript sort functiontypescript order an array sort 28 29 sort the number typescripttypescript resort arrayts order arraynodejs typescript sorting resultssort by number in typescriptsort list of array typescriptsort by number typescripttypscript array sortarray orderby typescriptsorting in typescriptsort array in tshow to order numbers with sort tssort array of numbers typescripttypscript sort arraytypescript sort numeric array sort 28 29 typescript exampletypescript array orderbysort an array of numbers angular exampletypescript sort then byhow to sort an array in ascending order in typescripthow to sort arraylist in angular ts filesort function typescriptts order array by valuesort array typescriptts array sort by functionssort method typescriptaplhabetize typescript varaiblessort data in typescriptorder list tsarray sort in typescriptorganize small to big the values of array angularelement number array typescrtipt sortorder by array typescripttypescript array sort 28typescript sort array of numberstypescript sort list switch sorting typescripthow to sort with typescripttypescript sort array of num 2cebnrssorted list typescriptsort data numerically in typescripttypescript sorting arraysort number in typescriptsorting data in typescripttypescript sorttypescript useing array sorttypescript sort datats sort array by numberascending order list tstypescript sort number ascending ordertypescript sort bytypescript array sort by numbersort list in typescriptsorting on list with types script 2b angulararray sorting in typescriptsorting in tstypescript sort arraytypescript sort typeaheadsort in typescript arraytypescript order arrayarray orderby in typescriptsort an array in typescriptjavascript sort with typescriptts sortby array sort 28 29 typescriptsort method in typescripttypescript sort listsort list typescripttypescript order bysorting list by typescriptarray sort typescripttypescript sort array on numbertypescript reorder arraydsort array tssort by numbers typescripthow to sort an array in typescriptarray sort typescript numbersbest way to sort array tstypescript sort ascendingtypescript sort arratsorting array in tssorting code in typescriptsort typescripttypescript orderbytypescript lamda order bysort function typescript typearray sorting in ascending order typescriptsort in typescripttypescript sort array by namejs array sort by number value typescripttypescript order array from valuesort array string tsordering the list in typescripttypescript how to sort an arraytypescript num array sort in ascending ordertype scripte sortinghow to sort numbers in typescripttypescript sortorderby 28 29 typescripttypescript sort numner arraytypescript sort string with numbersort arraylist in typescripttypescript sort stjavascript sort typescriptsort array on typescriptts sort arraysorting in typescript jshow to sort an array typescriptsort an array typescriptsort 28 29 in typescriptsort loop typescripttypescript sort number arrayarray is being sorted typescriptts ordered listsort array angular typescriptsort array in ascending order in typescriptrearrange array typescripttypescript angular sort arrayorder by in typescriptsort array of numbers tsarray ascending order in typescriptts array sorttypescript sorttypescript sort string numberstypescript order a numeric arrayts array order byorder array typescripthow typescript sortarray sort 28 29 typescriptarray sort example typescriptorder by array in typescripttypescript sort stringsts sortingtypescript number comparator sortsorting in react js with typescriptsort array by number typescriptsorting varchar in typescript arraysorting array in typescripttypescript array sortsort array in typescriptsort by value typescriptsort array of model typescriptsort type data typescripttypescript sort