typescript 具有空值和实际值的 lodash orderby 未正确排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41138820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
lodash orderby with null and real values not ordering correctly
提问by Ben Cameron
I have an Angular 2 typescript application that is using lodash for various things.
我有一个 Angular 2 打字稿应用程序,它使用 lodash 处理各种事情。
I have an array of objects that I am ordering using a property in the object...
我有一个对象数组,我使用对象中的属性进行排序...
_.orderBy(this.myArray, ['propertyName'], ['desc']);
This works well however my problem is that sometimes 'propertyName' can have a null value. These are ordered as the first item in a descending list, the highest real values then follow.
这很有效,但是我的问题是有时“propertyName”可以有一个空值。这些按降序排列为第一项,然后是最高的实际值。
I want to make these null values appear last in the descending ordering.
我想让这些空值按降序出现在最后。
I understand why the nulls come first.
我明白为什么空值首先出现。
Does anyone know how to approach this?
有谁知道如何解决这个问题?
采纳答案by Ben Cameron
The code I needed looks like this...
我需要的代码看起来像这样......
_.orderBy(this.myArray, [( o ) => { return o.myProperty || ''}], ['desc']);
回答by Ori Drori
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.
该_.orderBy()
功能的iteratees可以使用,而不是一个字符串的方法。检查值,如果它null
返回一个空字符串。
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:
如果您需要更严格的检查,您可以使用三元运算符,并仅处理null
值:
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
.
这将在propertyName
那时订购propertyName2
。
If
propertyName
is undefined/null then its default order will be set to0
. (and therefore will be displayed at last because ofdesc
ordering on thepropertyName
field). In such case,propertyName2
will therefore determine the ordering.
如果
propertyName
是 undefined/null 那么它的默认顺序将被设置为0
。(因此将最终显示,因为desc
在propertyName
场上排序)。在这种情况下,propertyName2
将因此确定排序。
回答by infinity
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']
)
回答by Stephen
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']
)
回答by lars1595
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] : "";
};
})),
);
回答by Johansrk
mine looks like this. PropName and sort are both variables in my solution
我的看起来像这样。PropName 和 sort 都是我的解决方案中的变量
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
我想小写,否则如果不同的大小写排序不正确