This is a migrated thread and some comments may be shown as answers.

Sorting Grid Columns with some values null

8 Answers 1111 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Guido
Top achievements
Rank 1
Guido asked on 27 Jun 2012, 04:41 PM
If I have a column with values like this

a
e
null
null
b
f

And toggle sort on that column, only the values above null are affected

e
a
null
null
b
f

Can null values be read as empty strings/ or some value that places them on the bottom when sorting
Can the sort function be replaced with a custom sort function?

8 Answers, 1 is accepted

Sort by
0
Guido
Top achievements
Rank 1
answered on 27 Jun 2012, 07:04 PM
I hate having to edit the source, but this is my solution for now. I've edited the asc and desc functions in var Comparer. I've added null checks.

asc: function(field) {
    var selector = this.selector(field);
    return function (a, b) {
        a = selector(a);
        b = selector(b);
        if (a === null) {
            if (b === null) {
                return 0;
            }
            return 1;
        }
        if (b === null) {
            return -1;
        }
        return a > b ? 1 : (a < b ? -1 : 0);
    };
},
 
desc: function(field) {
    var selector = this.selector(field);
    return function (a, b) {
        a = selector(a);
        b = selector(b);
        if (a === null) {
            if (b === null) {
                return 0;
            }
            return -1;
        }
        if (b === null) {
            return 1;
        }
        return a < b ? 1 : (a > b ? -1 : 0);
    };
},
0
Erlend
Top achievements
Rank 2
answered on 19 Sep 2012, 01:49 PM
Is there no other way, than edit source?
This should be last resort, as this will create issues on upgrades and using minified versions.

Telerik Support Staff!, are you able to give a better guidance here?

Thanks
0
Joel
Top achievements
Rank 1
answered on 13 Jan 2015, 01:22 AM
This is a very important feature. Is there still a solution to this that doesn't require editing the source?
0
Kiril Nikolov
Telerik team
answered on 15 Jan 2015, 05:09 PM

Hello Joel,

It works correctly with the latest version. Please see the example below:

http://dojo.telerik.com/oYaTa

Or am I missing something? 

Regards,
Kiril Nikolov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Joel
Top achievements
Rank 1
answered on 15 Jan 2015, 05:31 PM
No, that's not working correctly because the null value was able to reach the top of the sort.
0
Joel
Top achievements
Rank 1
answered on 15 Jan 2015, 05:33 PM
The behavior that is wanted in this case is to group all null/empty values at the bottom of the sort, no matter if it's sorting in ascending or descending. Null values should always be at the bottom since they do not contribute anything in an ascending sort.
0
Joel
Top achievements
Rank 1
answered on 15 Jan 2015, 05:41 PM
Here is a wonderful example: http://jsfiddle.net/CYubV/
0
Kiril Nikolov
Telerik team
answered on 20 Jan 2015, 11:28 AM
Hello Joel,

If you are not happy with the current sorting algorithm you can declare your own custom sorter, and handle your scenario. Here is some documentation and example that might help:

http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#configuration-columns.sortable.compare

Regards,
Kiril Nikolov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Guido
Top achievements
Rank 1
Answers by
Guido
Top achievements
Rank 1
Erlend
Top achievements
Rank 2
Joel
Top achievements
Rank 1
Kiril Nikolov
Telerik team
Share this question
or