Sort not guessing integer sort?
From what I have read in the docs, sort is supposed to look at the data in a column, and guess what type of sort algorithm to apply - strings, reals, integers and so forth - right?
In this plunker there is a column of only integers, but it gets sorted alphabetically - 11 appears between 1 and 2, etc. Is there something more I should be doing to get the sort to guess the sort type, or is this a failure of the guessing?
And in the meantime, how do I force the various pre-defined sorts? I don't quite understand the docs on this matter.
thanks
John
I'm pretty sure the guessing is based on typeof. So a number represented as a string will still get the string sort. You can set a type in the columnDef, I just can't recall the valid values, but the sort code would give you some hints.
It would be nice if it used more than typeof. I have data coming in via json, and I don't know in advance what type of data it will be - the field I am tabulating uses various validation regexes to impose type on the data, but always stores it as a string. If the guessing is only using typeof, I'll just have to add code to the validation, that triggers the correct sort in columnDef. No biggie. It's probably safer than relying on a guessing algorithm anyway.
In the docs for columnDef, i can't see where you can set the sort type, and nor can I see it in the code.
The function that sets the sort type, rowSorter.getSortFn, 1) checks for an existing sort algorithm in the cache 2) checks for a user-supplied algorithm 3) tries to guess the algorithm and, failing all that, 4) defaults to alpha sort. As far as I can tell, nowhere does it look to see if the user has chosen one of the predefined algorithms.
So it seems my only option is to supply my own algorithms? Since I would only be cutting and pasting the pre-defined ui-grid algorithms into my own code in order to feed them back to ui-grid to trigger 2), it seems like there should be a better way to go . . .
rowSorter.guessSortFn = function guessSortFn(itemType) {
switch (itemType) {
case "number":
return rowSorter.sortNumber;
case "boolean":
return rowSorter.sortBool;
case "string":
return rowSorter.sortAlpha;
case "date":
return rowSorter.sortDate;
case "object":
return rowSorter.basicSort;
default:
throw new Error('No sorting function found for type:' + itemType);
}
};
Although I haven't tested it, my assumption was that this code was looking at a type column in the columnDef. This line of code seems to align with that assumption:
sortFn = rowSorter.guessSortFn(col.colDef.type);
I had assumed that you could set colDef.type to, say, 'number', and it would then use that sorter.
I missed that one... thanks. Given you have a very nice "sortNumberStr" algorithm, that appears to strip off currency symbols, commas etc and sort the underlying number (something I would use almost all the time) it would be nice if that were an option, too. I presume that's just an oversight?
(And, yes, I just tested it, and it does what you presumed it does. When I read that code, I presumed it was taking taking itemType from typeof, but I was mistaken. Which now has me wondering where it does the actual guessing, given it's not a guess but a chosen option. But nevermind .)
Ah, the sortNumberStr I hadn't noticed before. Happy to take a PR for it, not sure whether we'd use it as a type (so support a 'numberStr' type in the columnDef) or whether we'd use it where there was a column of type number but the actual data type was a string. Probably the former is safer.
EDIT: On the type, I think the type is set somewhere (in the column maybe?) based on either you defining a type, or otherwise it looks at the first row of data and guesses the type. So I think ultimately if you follow the chain back it is using typeof.
FURTHER EDIT:
Grid.prototype.assignTypes = function(){
var self = this;
self.options.columnDefs.forEach(function (colDef, index) {
//Assign colDef type if not specified
if (!colDef.type) {
var col = new GridColumn(colDef, index, self);
var firstRow = self.rows.length > 0 ? self.rows[0] : null;
if (firstRow) {
colDef.type = gridUtil.guessType(self.getCellValue(firstRow, col));
}
else {
gridUtil.logWarn('Unable to assign type from data, so defaulting to string');
colDef.type = 'string';
}
}
});
};
Looks like grid.js does it, and looks like it only works if there is data present. But it seems to do it again whenever we buildColumns, so probably it'll get types eventually.
Looks like we merged the resolution to this.....closing.
is there an analogous fix for filtering? currently, i am trying to use the less than filter to filter currency entries and it is filtering them like strings. if i enter 5 into the filter input field, it shows 1, 10, 11, 12, 120 etc instead of the numbers less than 5.
Yeah, if your values in the cell are all numbers, then it treats the filter condition as a number. You'd need to convert all your data input to values, and use a cellFilter to display the currency formatting. I think we do this in one of the tutorials - maybe edit or filter.
But the sorting sees the cell contents as numbers (arranges them into correct numerical order). Shouldn't the filtering too? I explicitly set the column type to 'number'.