How can I sort:
[[{path:'a/b/c'}],[{path:'a/b'}],[{path:'a/b/c/d'}],[{path:'a/b'}]]
into this:
[[{path:'a/b'}],[{path:'a/b'}],[{path:'a/b/c'}],[{path:'a/b/c/d'}]]
I want to sort it by segment length. For example, a/b/c is three segments.
Use split and count the length of the output array.
let toSort = [[{ path: 'a/b/c' }], [{ path: 'a/b' }], [{ path: 'a/b/c/d' }], [{ path: 'a/b' }]]; toSort.sort((a, b) => { return a[0].path.split('/').length - b[0].path.split('/').length }); console.log(toSort);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.