diff --git a/src/schema.ts b/src/schema.ts index 1cd837dd..750a07b8 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -8,7 +8,7 @@ import * as dlBin from 'datalib/src/bins/bins'; import {BinQuery, EncodingQuery} from './query/encoding'; import {QueryConfig, DEFAULT_QUERY_CONFIG} from './config'; -import {contains, extend, keys} from './util'; +import {cmp, contains, extend, keys} from './util'; export class Schema { private _fieldSchemas: FieldSchema[]; @@ -255,11 +255,14 @@ export class Schema { fieldSchema.primitiveType === PrimitiveType.NUMBER) { // coerce non-quantitative numerical data into number type domain = domain.map(x => +x); - return domain.sort(function(a, b) { - return a - b; - }); + return domain.sort(cmp); } - return domain.sort(); + + return domain.map((x) => { + // Convert 'null' to null as it is encoded similarly in datalib. + // This is wrong when it is a string 'null' but that rarely happens. + return x==='null' ? null : x; + }).sort(cmp); } /** diff --git a/src/util.ts b/src/util.ts index b3d3e51b..73a585f4 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,5 +1,5 @@ import {isArray} from 'datalib/src/util'; -export {keys, duplicate, extend, isObject, isArray} from 'datalib/src/util'; +export {cmp, keys, duplicate, extend, isObject, isArray} from 'datalib/src/util'; export interface Dict { [key: string]: T; diff --git a/typings/datalib.d.ts b/typings/datalib.d.ts index f55dc0c0..022fc592 100644 --- a/typings/datalib.d.ts +++ b/typings/datalib.d.ts @@ -4,6 +4,7 @@ declare module 'datalib/src/import/load' { } declare module 'datalib/src/util' { + export function cmp(a, b): number; export function keys(a): Array; export function extend(a, b, ...rest); export function duplicate(a: T): T;