lodash orderby with null and real values not ordering correctly

后端 未结 6 717

I have an Angular 2 typescript application that is using lodash for various things.

I have an array of objects that I am ordering using a property in the object...

相关标签:
6条回答
  • 2021-01-07 22:47

    The _.orderBy() function's iteratees can use a method instead of a string. Check the value, and if it's null return an empty string.

    const myArray = [{ propertyName: 'cats' }, { propertyName: null }, { propertyName: 'dogs' }, { propertyName: 'rats' }, { propertyName: null }];
    
    const result = _.orderBy(myArray, ({ propertyName }) => propertyName || '', ['desc']);
    
    console.log(result);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>

    The check can be simple (like the one I've used), which converts all falsy values to an empty string:

    propertyName || ''
    

    If you need a stricter check, you can use the ternary operator, and handle just null values:

    propertyName === null ? '' : propertyName
    

    Edit: Example with multiple ordering:

    const result = _.orderBy(myArray, (item) => [get(item, 'propertyName', 0), get(item, 'propertyName2')], ['desc', 'asc']);
    

    This will order by propertyName then propertyName2.

    If propertyName is undefined/null then its default order will be set to 0. (and therefore will be displayed at last because of desc ordering on the propertyName field). In such case, propertyName2 will therefore determine the ordering.

    0 讨论(0)
  • 2021-01-07 22:52

    The code I needed looks like this...

    _.orderBy(this.myArray, [( o ) => { return o.myProperty || ''}], ['desc']); 
    
    0 讨论(0)
  • 2021-01-07 23:03

    This worked for me

    orders = [{id : "1", name : "test"}, {id : "1"}];
    sortBy = ["id", "name"];
    orderby(
                orders,
                sortBy.map(s => {
                    return (r: any) => {
                        return r[s] ? r[s] : "";
                    };
                })),
            );
    
    0 讨论(0)
  • 2021-01-07 23:04

    Just for future reference to others you can do this to sort ascending with falsey values at the end.

    items =>
      orderBy(
        items,
        [
          i => !!i.attributeToCheck,
          i => {
            return i.attributeToCheck ? i.attributeToCheck.toLowerCase() : ''
          }
        ],
        ['desc', 'asc']
      )
    
    0 讨论(0)
  • 2021-01-07 23:04

    This will put bad values at the bottom, and it differentiates between numbers and strings.

    const items = [] // some list
    
    const goodValues = isAscending => ({ value }) => {
        if (typeof value !== 'string' && isNaN(value)) {
            return isAscending ? Infinity : -Infinity
        }
    
        return value || ''
    }
    
    const sortedItems = orderBy(
        items,
        [goodValues(isAscending), 'value'],
        [isAscending ? 'asc' : 'desc']
    )
    
    0 讨论(0)
  • 2021-01-07 23:08

    mine looks like this. PropName and sort are both variables in my solution

    return _.orderBy( myarray, [
      ( data ) => {
        if ( data[propName] === null ) {
            data[propName] = "";
        }
        return data[propName].toLowerCase();
        }
     ], [sort] );
    

    I wanted tolowercase because otherwise the sorting is not correct if different casings

    0 讨论(0)
提交回复
热议问题