fordth / jinqJs

jinqJs provides a simple way to perform SQL like queries on javaScript arrays, collections and web services using LINQ expressions.
Other
93 stars 29 forks source link

Sort failed if data is an empty string #25

Open rongxike opened 7 years ago

rongxike commented 7 years ago
(function() { 
  angular.module('app', ['ui.grid'])
                   .controller('demoCtrl', ['$scope', function ($scope) {

    var data1 = [ {Name: 'Tom', Age: 'a', Location: 'Port Jeff', Sex: 'Male'},
              {Name: 'Jen', Age: '', Location: 'Port Jeff', Sex: 'Female'},
              {Name: 'Tom', Age: 'b', Location: 'Port Jeff', Sex: 'Male'},
              {Name: 'Diana', Age: '', Location: 'Port Jeff', Sex: 'Female'} ];

    $scope.data = new jinqJs()
               .from(data1)
               .orderBy([{field: 'Age', sort: 'desc'}])
               .select('Name', 'Age', 'Sex');
  }]);  
}());

result:

'a'
''
'b'
''

https://jsfiddle.net/rongxike/r7Lc22ta/

To fix this error:

orderByComplex = function (complexFields) {
...
lValue = (field === null ? first : (isNaN(first[firstField]) || first[firstField] == '' ? first[firstField] : Number(first[firstField])));
                    rValue = (field === null ? second : (isNaN(second[secondField]) || second[secondField] == '' ? second[secondField] : Number(second[secondField])));
...
}
fordth commented 7 years ago

Thanks! I will run it through the unit tests.

mcoakley commented 7 years ago

I've added this fix and a regression test to my fork (https://github.com/mcoakley/jinqJs) in which I'm making the testing changes.

fordth commented 7 years ago

Great!