Data:
16.00 hours 19.99 hours 210.46 hours 262.54 hours 303.19 hours 55.95 hours 60.07 hours 64.07 hours 7.95 hours
What I want:
I need to allow sorting for this king of data in koGrid (default feature by clicking grid header, asc or desc sort).
Problem:
The data is string so you might guess how it sorts out - by char codes. What are my options? I found that a column can have sortFn assigned, something like this I guess?
Code:
this.gridOptions = {
canSelectRows: false,
showColumnMenu: false,
showFilter: false,
columnDefs: [
{ field: 'localizedName', displayName: 'Name' },
{
field: 'localizedDuration', displayName: 'Duration',
sortFn: function (itemA: string, itemB: string) {
var numA = parseFloat(itemA);
var numB = parseFloat(itemB);
if (!numA && !numB)
return 0;
else if (!itemA && (itemA === undefined || itemA === null))
return 1;
else if (!itemB && (itemB === undefined || itemB === null))
return -1;
if (numA > numB)
return -1;
else if (numA < numB)
return 1;
return 0;
}
},
],
data: this.dataRows
};
Fiddle: JSFiddle
Just try to modify your code like this
View :
viewModel:
Working fiddle here
Reference documentation available here