Skip to content

Commit

Permalink
Sorting: String sorting converts values to string
Browse files Browse the repository at this point in the history
Fixes #93
  • Loading branch information
tabalinas committed Aug 18, 2015
1 parent 6bbea96 commit 86f1cd6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/jsgrid.sort-strategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
if(!isDefined(str2))
return 1;

return str1.localeCompare(str2);
return ("" + str1).localeCompare("" + str2);
},

number: function(n1, n2) {
Expand Down
94 changes: 51 additions & 43 deletions tests/jsgrid.sort-strategies.tests.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,51 @@
$(function() {

var sortStrategies = jsGrid.sortStrategies;


module("sortStrategies");

test("string sorting", function() {
var data = ["c", "a", "d", "b"];

data.sort(sortStrategies["string"]);

deepEqual(data, ["a", "b", "c", "d"]);
});

test("number sorting", function() {
var data = [5, 3.2, 1e2, 4];

data.sort(sortStrategies["number"]);

deepEqual(data, [3.2, 4, 5, 100]);
});

test("date sorting", function() {
var date1 = new Date(2010, 0, 1),
date2 = new Date(2011, 0, 1),
date3 = new Date(2012, 0, 1);

var data = [date2, date3, date1];

data.sort(sortStrategies["date"]);

deepEqual(data, [date1, date2, date3]);
});

test("numberAsString sorting", function() {
var data = [".1", "2.1", "4e5", "2"];

data.sort(sortStrategies["numberAsString"]);

deepEqual(data, [".1", "2", "2.1", "4e5"]);
});
});
$(function() {

var sortStrategies = jsGrid.sortStrategies;


module("sortStrategies");

test("string sorting", function() {
var data = ["c", "a", "d", "b"];

data.sort(sortStrategies["string"]);

deepEqual(data, ["a", "b", "c", "d"]);
});

test("string sorting should be robust", function() {
var data = ["a", 1, true, "b"];

data.sort(sortStrategies["string"]);

deepEqual(data, [1, "a", "b", true]);
});

test("number sorting", function() {
var data = [5, 3.2, 1e2, 4];

data.sort(sortStrategies["number"]);

deepEqual(data, [3.2, 4, 5, 100]);
});

test("date sorting", function() {
var date1 = new Date(2010, 0, 1),
date2 = new Date(2011, 0, 1),
date3 = new Date(2012, 0, 1);

var data = [date2, date3, date1];

data.sort(sortStrategies["date"]);

deepEqual(data, [date1, date2, date3]);
});

test("numberAsString sorting", function() {
var data = [".1", "2.1", "4e5", "2"];

data.sort(sortStrategies["numberAsString"]);

deepEqual(data, [".1", "2", "2.1", "4e5"]);
});
});

0 comments on commit 86f1cd6

Please sign in to comment.