Comments (7)
warning:
You should have a reasonable initialization parameter,otherwise the result may not be what you want.
see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator
Example:
var cf = new Intl.Collator();
cf.compare(1,2)
//=-1
cf.compare(15,2)
//=-1 (Expected = 1)
from linq.
This is the function used for comparison:
compare: function (a, b) {
return (a === b) ? 0
: (a > b) ? 1
: -1;
}
Values are reversed if (a>b)
.
Unfortunately comparing something with null always returns false.
console.log('2019-09-12' > null)
false
console.log('2019-09-12' < null)
false
One solution would be to pass a better lambda to orderBy
, causing the value to be sanitized before comparing:
const result = Enumerable.from(data).orderBy(x => (x===null) ? "" : x).toArray();
from linq.
Thank you for your quick reply. Do you intend to change the library to better handle nulls like in this case? Or is the workaround you posted going to be the permanent solution?
from linq.
Not sure yet...
array.sort()
does indeed sort the above values but it does so by converting nulls to strings.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
We can probably do the same for this library using some typeof
wizzardry, but it feels like a very specific scenario.
from linq.
Gotcha. It's a fairly common use case for us ( e.g. calling .select(...)
to grab a nullable property then sorting it using .orderBy(...)
).
We were surprised that the order returned was not the same as when doing the equivalent calls in C# LINQ. C# will return the expected output I listed earlier.
from linq.
This is the function used for comparison:
compare: function (a, b) { return (a === b) ? 0 : (a > b) ? 1 : -1; }
Values are reversed if
(a>b)
.
Unfortunately comparing something with null always returns false.console.log('2019-09-12' > null) false console.log('2019-09-12' < null) false
One solution would be to pass a better lambda to
orderBy
, causing the value to be sanitized before comparing:
const result = Enumerable.from(data).orderBy(x => (x===null) ? "" : x).toArray();
I suddenly realized that Intl could solve this problem
from linq.
Now , you can set compare function , at 3.2.2
Issue: #74
const a= ['2019-10-01', null, '2019-09-12', '2019-09-15', null];
var cf = new Intl.Collator();
Enumerable.from(a).orderBy(x=>x, cf.compare).toArray()
output:
["2019-09-12", "2019-09-15", "2019-10-01", null, null]
You can prepare a custorm compare function to let null in front.
from linq.
Related Issues (20)
- Typescript, generic Linq functions
- Enhancement: Async / Await functionality
- Contains Method does not Delivers True HOT 2
- Question: How to use Join Correctly? HOT 1
- Can't the groupby method groupby multiple keys?
- How to use linq in nested of nested object
- Does it support iterables? HOT 1
- Unexpected behavior in [Symbol.iterator] HOT 1
- hope to provide example of like groupby in typescript.
- Add toMap HOT 1
- Type inference for .toObject HOT 3
- how to use select with chained groupby HOT 1
- Overloaded `all` with extra predicate args like `every`
- how to use left join? HOT 2
- Help: OrderByDescending chain with dates not triggereing second orderBy function (but ok if type is number) HOT 2
- Missing IDictionary.key and IDictionary.values
- Enumerable.from is missing Array[] overload. HOT 1
- Transfer ownership of this package HOT 3
- Iterable support HOT 7
- How to extend Enumerable (IEnumerable?) HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from linq.