I’m implementing SlickGrid and I want to allow sorting of values by columns, which is supported by SlickGrid by sorting arrays, however, when I sort the columns (arrays) they are not put in the “correct” order.
The order in which they are returned is 1,10,100,11,199,2,20,200,3,30,300….
The problem is displayed very clearly when trying to sort the tasks in this grid by title:
http://mleibman.github.com/SlickGrid/examples/example-multi-column-sort.html
Although I use my own sorting rule, instead of the one used in the example:
data.sort(function(a, b){
var result =
a[field] === b[field] ? 0 :
a[field] > b[field] ? 1 : -1
;
return args.sortAsc ? result : -result;
});
The problem persists.
My question is merely how to sort the array, so that the title (and other data) will be displayed in the correct order: 1,2,3,100,200,300…
Your numbers are actually strings and will be compared as such. To prevent this, use
parseInt(a[field],10) > parseInt(b[field],10)