From 66b451424408ce0ca1669c73e2e6895f04590c09 Mon Sep 17 00:00:00 2001 From: christophe-g Date: Sat, 4 Feb 2017 18:12:50 +0100 Subject: [PATCH 1/2] sort function handled undefined, null or empty string values --- array-datasource.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/array-datasource.js b/array-datasource.js index a326521..6b17c78 100644 --- a/array-datasource.js +++ b/array-datasource.js @@ -19,13 +19,7 @@ function ArrayDataSource(arr) { } function _compare(a, b) { - if (a < b) { - return -1; - } - if (a > b) { - return 1; - } - return 0; + return (a===undefined)-(b===undefined) || (a==='')-(b==='') || (a===null)-(b===null) || +(a>b)||-(a Date: Fri, 21 Apr 2017 21:00:52 +0300 Subject: [PATCH 2/2] Add tests for empty sorting --- test/sorting.html | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/sorting.html b/test/sorting.html index 2ccc666..8bc2e78 100644 --- a/test/sorting.html +++ b/test/sorting.html @@ -129,6 +129,50 @@ expect(columnSort.direction).to.eql('desc'); }); }); + + describe('sorting empty values', function() { + beforeEach(function() { + }); + + it('should sort null values', function(done) { + grid.items = [{value: 'foo'}, {value: null}, {value: 'bar'}]; + + grid.push('sortOrder', {path: 'value', direction: 'asc'}); + + grid.async(function() { + expect(grid.$.list.items[0]).to.eql({value: 'bar'}); + expect(grid.$.list.items[1]).to.eql({value: 'foo'}); + expect(grid.$.list.items[2]).to.eql({value: null}); + done(); + }, 100); + }); + + it('should sort undefined values', function(done) { + grid.items = [{value: 'foo'}, {value: undefined}, {value: 'bar'}]; + + grid.push('sortOrder', {path: 'value', direction: 'asc'}); + + grid.async(function() { + expect(grid.$.list.items[0]).to.eql({value: 'bar'}); + expect(grid.$.list.items[1]).to.eql({value: 'foo'}); + expect(grid.$.list.items[2]).to.eql({value: undefined}); + done(); + }, 100); + }); + + it('should sort empty string values', function(done) { + grid.items = [{value: 'foo'}, {value: ''}, {value: 'bar'}]; + + grid.push('sortOrder', {path: 'value', direction: 'asc'}); + + grid.async(function() { + expect(grid.$.list.items[0]).to.eql({value: 'bar'}); + expect(grid.$.list.items[1]).to.eql({value: 'foo'}); + expect(grid.$.list.items[2]).to.eql({value: ''}); + done(); + }, 100); + }); + }) });