ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

Sorting works inconsistently , when table column contains null/undefined/empty values

Open NiveditaPastor opened this issue 9 years ago • 15 comments

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 -

  1. Step 1 : Ascending order - null values are pushed at the bottom, values gets sorted correctly
  2. Step 2 : Descending order - nothing happens ,sorting does not work.

NiveditaPastor avatar Apr 24 '15 13:04 NiveditaPastor

Are there any errors on the javascript console when it fails to work? Are you able to provide a plunker?

PaulL1 avatar May 15 '15 23:05 PaulL1

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

NiveditaPastor avatar May 27 '15 13:05 NiveditaPastor

@PaulL1 Hey Paul , did you get time to look at this issue ? I have provided plunker example above.

NiveditaPastor avatar Jun 15 '15 13:06 NiveditaPastor

I've been watching this issue for months now. Do we have a fix for this yet ?

techieanant avatar Nov 03 '15 17:11 techieanant

No, I haven't had time to look at it, but may be able to do so soon.

PaulL1 avatar Nov 03 '15 18:11 PaulL1

I have the same issue.

aalesar avatar Nov 05 '15 10:11 aalesar

I also have the same issue.

balakrishnangithub avatar Dec 16 '15 12:12 balakrishnangithub

I too have the same issue.

patimanas321 avatar Aug 18 '16 15:08 patimanas321

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:

miseeger avatar Aug 26 '16 09:08 miseeger

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

dalbir avatar Jan 17 '17 21:01 dalbir

this does not seem to work when grouping by that column the null values still go to the bottom in ASC

goleafs avatar Oct 05 '17 04:10 goleafs

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.

a3aakash avatar Mar 28 '18 07:03 a3aakash

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

Howzilla avatar Aug 26 '19 16:08 Howzilla

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 avatar Sep 26 '19 02:09 cyimking

@cyimking Thank you, man! I was missing this '||' condition. It did the trick for me.

AsifSajid avatar Jul 30 '21 15:07 AsifSajid