TypeScript array sorting

I tried to figure out a very strange problem that I ran into typescript. It treated the built-in Boolean expression as any first type value instead of the full expression.

So, if you try something simple, like the following:

var numericArray:Array<number> = [2,3,4,1,5,8,11]; var sorrtedArray:Array<number> = numericArray.sort((n1,n2)=> n1 > n2); 

Tryit

You will receive an error message in your sorting method, indicating that the parameters do not match any signature of the target, because your result is numeric, not logical. I assume that I am missing something, but I am sure that n1> n2 is a logical expression.

+104
typescript
Feb 10
source share
6 answers

The error is completely correct.

As he tries to tell you, .sort() takes a function that returns a number, not a boolean.

You need to return a negative result if the first element is less; positive if it is greater, or zero if they are equal.

+134
Feb 10 '14 at 21:08
source share

When sorting numbers, you can use compact comparison:

 var numericArray: number[] = [2, 3, 4, 1, 5, 8, 11]; var sortedArray: number[] = numericArray.sort((n1,n2) => n1 - n2); 

i.e. - , not < .

If you are comparing anything else, you need to convert the comparison to a number.

 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; }); 
+166
Feb 10 '14 at 22:31
source share

Great answer Sohnee. I would like to add that if you have an array of objects and you want to sort by key, then this is almost the same, this is an example of one that can be sorted by date (number) or title (string):

  if (sortBy === 'date') { return n1.date - n2.date } else { if (n1.title > n2.title) { return 1; } if (n1.title < n2.title) { return -1; } return 0; } 

You can also make the values ​​inside as variables n1 [field] vs n2 [field], if it is more dynamic, just save the difference between strings and numbers.

+21
Sep 10 '16 at 6:56
source share
 let numericArray: number[] = [2, 3, 4, 1, 5, 8, 11]; let sortFn = (n1 , n2) => number { return n1 - n2; } const sortedArray: number[] = numericArray.sort(sortFn); 

Sort by some field:

 let arr:{key:number}[] = [{key : 2}, {key : 3}, {key : 4}, {key : 1}, {key : 5}, {key : 8}, {key : 11}]; let sortFn2 = (obj1 , obj2) => {key:number} { return obj1.key - obj2.key; } const sortedArray2:{key:number}[] = arr.sort(sortFn2); 
+9
Jan 29 '18 at 13:07 on
source share

Sort mixed array (letters and numbers)

 function naturalCompare(a, b) { var ax = [], bx = []; a.replace(/(\d+)|(\D+)/g, function (_, $1, $2) { ax.push([$1 || Infinity, $2 || ""]) }); b.replace(/(\d+)|(\D+)/g, function (_, $1, $2) { bx.push([$1 || Infinity, $2 || ""]) }); while (ax.length && bx.length) { var an = ax.shift(); var bn = bx.shift(); var nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]); if (nn) return nn; } return ax.length - bx.length; } let builds = [ { id: 1, name: 'Build 91'}, { id: 2, name: 'Build 32' }, { id: 3, name: 'Build 13' }, { id: 4, name: 'Build 24' }, { id: 5, name: 'Build 5' }, { id: 6, name: 'Build 56' } ] let sortedBuilds = builds.sort((n1, n2) => { return naturalCompare(n1.name, n2.name) }) console.log('Sorted by name property') console.log(sortedBuilds) 

+7
Jul 05 '18 at 12:18
source share

It seems the easiest way is to subtract the second number from the first:

 var numericArray:Array<number> = [2,3,4,1,5,8,11]; var sorrtedArray:Array<number> = numericArray.sort((n1,n2) => n1 - n2); 

https://alligator.io/js/array-sort-numbers/

+6
Oct 19 '18 at 17:42
source share



All Articles