admin管理员组文章数量:1393908
I am using angular 10
and I was wondering how can I sort this array
var dic = [
{ state: false, id: 1 },
{ state: true, id: 2} ,
{ state: false, id: 3 },
{ state: true, id: 4 },
{ state: false, id: 5 }
]
I want to sort by the value of the boolean state
, so the result goes this way:
[
{ state: true, id: 2 },
{ state: true, id: 4 },
{ state: false, id: 1 },
{ state: false, id: 3 },
{ state: false, id: 5 }
]
The true
value goes first in the array.
What property or something from typescript I have to use to do that?
Thank you!
I am using angular 10
and I was wondering how can I sort this array
var dic = [
{ state: false, id: 1 },
{ state: true, id: 2} ,
{ state: false, id: 3 },
{ state: true, id: 4 },
{ state: false, id: 5 }
]
I want to sort by the value of the boolean state
, so the result goes this way:
[
{ state: true, id: 2 },
{ state: true, id: 4 },
{ state: false, id: 1 },
{ state: false, id: 3 },
{ state: false, id: 5 }
]
The true
value goes first in the array.
What property or something from typescript I have to use to do that?
Thank you!
Share Improve this question edited Apr 16, 2021 at 1:37 Nguyễn Văn Phong 14.2k19 gold badges46 silver badges63 bronze badges asked Apr 15, 2021 at 19:12 Luis BermúdezLuis Bermúdez 65413 silver badges32 bronze badges 1- 1 developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – tkausl Commented Apr 15, 2021 at 19:13
3 Answers
Reset to default 4You can do this using Array#sort
by converting the boolean
values of state
:
Number(true) //1
Number(false) //0
const dic = [
{ state: false, id: 1 },
{ state: true, id: 2 },
{ state: false, id: 3 },
{ state: true, id: 4 },
{ state: false, id: 5 }
];
dic.sort(({ state: stateA = false }, { state: stateB = false }) =>
Number(stateB) - Number(stateA)
);
console.log(dic);
You can plete it by doing simplify like this.
dic.sort((a, b) => b.state - a.state);
Explain:
We all know that:
false // 0
true // 1
So
false - true // -1
true - false // 1
true - true // 0
false - false // 0
Demo
const dic = [
{ state: false, id: 1 },
{ state: true, id: 2 },
{ state: false, id: 3 },
{ state: true, id: 4 },
{ state: false, id: 5 }
];
dic.sort((a, b) => b.state - a.state);
console.log(dic);
You can sort by the state by casting the boolean to an integer value of 1
or 0
by appending a double-bang (!!
) or double-tilde (~~
). If the result is 0
, move onto the id
values.
const dic = [
{ state: false , id: 1 },
{ state: true , id: 2 },
{ state: false , id: 3 },
{ state: true , id: 4 },
{ state: false , id: 5 }
];
dic.sort(({ state: stateA, id: idA }, { state: stateB, id: idB }) =>
(!!stateB - !!stateA) || (idA - idB));
console.log(dic);
.as-console-wrapper { top: 0; max-height: 100% !important; }
Alternatively:
(~~stateB - ~~stateA) || (idA - idB)
本文标签: javascriptsort an array by a boolean property in typescriptStack Overflow
版权声明:本文标题:javascript - sort an array by a boolean property in typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744082584a2587922.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论