CollectionView.sortNullsFirst immutable after setting it to true.

Posted by: license on 18 March 2020, 10:05 pm EST

  • Posted 18 March 2020, 10:05 pm EST

    Hello,

    When I'm instantiating a new CollectionView and I want to sort the collection with nulls sorted first I set sortNullsFirst to true.
    But if I want to change it back to false, the property doesn't change. I have to instantiate a new CollectionView with the same data or set the _nullsFirst property to false. Which I'd like to avoid.

    Is it intended that the sortNullsFirst property is immutable after being set to true?

    I would like to make use of the sortNullsFirst in the following way:
    When we sort our collection in an Ascending direction, the null values would be at the top. When sorting in a Descending direction the null values would be at the bottom.
    I'm subscribing to the sortingColumn event on the FlexGrid and changing the property based on the sort direction. But it's not working since sortNullsFirst can't be changed to false, once it's set to true.

    Wijmo version: 5.20192.631
    Angular verison: 8.2.12

    Cheers.
  • Marked as Answer

    Replied 19 March 2020, 4:17 pm EST

    Hi Rasmus,

    The value of sortNullsFirst depends upon the useStableSort, if the useStableSort is false, the value of sortNullsFirst cannot be false, except when it is set by default.
    To change the sortNullsFirst to false, you need to set the useStableSort to true.

    But, the sortNullsFirst will not fulfill your requirements because then the nulls will be sorted first regardless of the sort order. But, you want to sort the nulls at last while sorting in descending order. To achieve this, you will need to handle the sortComparer property of the CollectionView and manually sort the nulls:
    this.source.sortComparer = function(a, b) {
    if(a === null) {
    return -1;
    }
    else if(b === null) {
    return 1;
    }
    }

    You may refer to the sample below:

    https://stackblitz.com/edit/angular-6yy2jq

    Regards,
    Ashwin
  • Replied 19 March 2020, 4:58 pm EST

    Hello Ashwin,

    Thank you for the quick answer, I'll adjust accordingly.

    Cheers.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels