ui-grid
ui-grid copied to clipboard
Sorting works inconsistently , when table column contains null/undefined/empty values
I observed that the handlenulls method present in all sort methods(exmaple : alphasort) is working inconsistently. It is unable to handle the empty values in present in the table while sorting. Note - I am using Sort Ascending and Sort Descending options and not the header options.
Scenario - When a column containing null values is sorted -
- Step 1 : Ascending order - null values are pushed at the bottom, values gets sorted correctly
- Step 2 : Descending order - nothing happens ,sorting does not work.
Are there any errors on the javascript console when it fails to work? Are you able to provide a plunker?
There are no errors on console.Here I have made the changes and added empty rows in the tables. Now when you will sort using "Sort Ascending" or "Sort Descending" drop down actions , they are not working as expected. Please let me know if more information is needed.
http://plnkr.co/edit/Vpdn95eOUA5AD4If53FY?p=preview
@PaulL1 Hey Paul , did you get time to look at this issue ? I have provided plunker example above.
I've been watching this issue for months now. Do we have a fix for this yet ?
No, I haven't had time to look at it, but may be able to do so soon.
I have the same issue.
I also have the same issue.
I too have the same issue.
I ran into the same problem using ui-grid v3.2.6 as of 2016-07-14. So I created a quick workaround for this. It uses the sortingAlgorithm
option (on a clumn def) which refers to a custom sort function.
For my nullable date columns:
function sortNullDate(a, b) {
a = a === null ? '1899-12-31 23:59:59' : a;
b = b === null ? '1899-12-31 23:59:59' : b;
if (a === b) { return 0 };
if (a < b) { return -1 };
if (a > b) { return 1 };
};
For my empty-string columns:
function sortNullString(a, b) {
a = a == '' ? ' ' : a;
b = b == '' ? ' ' : b;
if (a === b) { return 0 };
if (a < b) { return -1 };
if (a > b) { return 1 };
};
Usage:
{ name: 'myNullableDateField', displayName: 'Date', cellFilter: 'nullDate: "dd.MM.yyyy"', sortAlgorithm: sortNullDate }
In my project, this works for both directions, ASC and DESC. Please try it yourself. I hope it helps :smiley:
sortFn: function(a,b){return a > b} (see Sorting and Filtering) It may be renamed to 'sortingAlgorithm'.
https://github.com/angular-ui/ui-grid/wiki/Sorting
this does not seem to work when grouping by that column the null values still go to the bottom in ASC
I need the same thing in my project but needs null values on top when the column is in ascending order and null values on the bottom when the column is in descending order. Need functionality similar to Mysql query sorting.
None of the above solves this issue. For me a grid set to return 50 rows initial DESC sorting is correct. When I then click a button to load ALL records the DESC sorting puts records with NULLs first then DESC order for the column. Haven't found a work around. ui-grid v4.7.1
Not sure if this helps out or anything, but this is the custom compare function I used to sort columns with null or emptied values: ` private static compare(v1, v2) { if(v1 < v2 || !v1) { return -1; }
if(v1 > v2 || !v2) {
return 1;
}
return 0;
} `
@cyimking Thank you, man! I was missing this '||' condition. It did the trick for me.