OrderBy doesn't work
Hi,
I tried to run the follwing code:
let t :any[] = [{name:'royi'}, {name:'david'}, {name:'barak'}, {name:'moshe'}];
let o = asEnumerable(t).OrderBy(s=> s.name).ToArray();
console.log(o);
It print in the original order...
I also try to define name string propety in type script class and orderby this property also do nothing..
Thanks, Royi
It looks like sorting is not a trivial matter if done properly. Article here skims over few issues.
Hi,
when there will be fix?
thanks, royi
Family matters, will do as soon as possible
As a workaround, while I am fixing the issue, you may provide your own comparison function to fix the error in your code
Just experienced the same problem using OrderBy the first time for objects with string or date property.
I hope for a new year release until then we can use:
http://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript http://stackoverflow.com/questions/39348556/typescript-sort-object-by-object-in-array
I prefer the linq flavor ;-)
Hi, It seems the default comparer algorithm doesn't work with Javascript string http://stackoverflow.com/questions/51165/how-do-you-do-string-comparison-in-javascript
This fixes the problems. let t :any[] = [{name:'royi'}, {name:'david'}, {name:'barak'}, {name:'moshe'}]; let o = asEnumerable(t).OrderBy(s=> s.name, (a,b)=>(a.localeCompare(b)) ).ToArray(); console.log(o);
A generic comparer function that should work on numbers and strings
let comparer = <T> (a: T,b: T) : number => {
if (typeof(a) === 'string' && typeof(b) === 'string' ){
return (<string>a).localeCompare(<string>b);
} else {
return ( <any>a - <any>b );
}
}
But maybe this can be written better... it's my fourth week with Typescript.