skip to Main Content

I want o sort an array of objects with a specific order given from a sort priority text.

var sortPriority = "ramoklyxq";

items[
0:{data: "aizak", level: 'z'}
1:{data: "alice", level: 'r'}
2:{data: "bob", level: 'a'}
3:{data: "trinity", level: 'm'}
4:{data: "ella", level: 'z'}
5:{data: "John", level: 'z'}
6:{data: "bryan", level: 'z'}
7:{data: "sam", level: 'o'}
8:{data: "nick", level: 'k'}
9:{data: "angel", level: 'l'}
10:{data: "arla", level: 'z'}
11:{data: "marley", level: 'o'}
etc
]

i want to return as:

items[
0:{data: "alice", level: 'r'}
1:{data: "bob", level: 'a'}
2:{data: "trinity", level: 'm'}
3:{data: "marley", level: 'o'}
4:{data: "sam", level: 'o'}
5:{data: "nick", level: 'k'}
6:{data: "angel", level: 'l'}
7:{data: "aizak", level: 'z'}
8:{data: "arla", level: 'z'}
9:{data: "bryan", level: 'z'}
10:{data: "ella", level: 'z'}
11:{data: "John", level: 'z'}
etc
]

Is there any way to sort the list by data.level order from sortPriority level.
If there no level exists in the sortPriority will be added after as in the return example.

I use this function first to sort with level and after with data.

    for (let i = 0; i < items.length; i++) {
        var list = document.getElementById('holder');
        [...list.children]
            .sort((a, b) => a.data.level > b.data.level ? 1 : -1)
            .forEach(node => list.appendChild(node));
        for (let j = 0; j < items.length; j++) {
            var list = document.getElementById('holder');
            [...list.children]
                .sort((a, b) => a.data.data > b.data.data ? 1 : -1)
                .forEach(node => list.appendChild(node));
        }
    }

I need something like this function with the correct priority in ASC for each level’s results so i can append it to list as shows

Thanks in advance.

2

Answers


  1. You could take an object for the order and sort accordingly. For same priority sort by string.

    const
        sortPriority = "ramoklyxq",
        data = [{ data: "aizak", level: 'z' }, { data: "alice", level: 'r' }, { data: "bob", level: 'a' }, { data: "trinity", level: 'm' }, { data: "ella", level: 'z' }, { data: "John", level: 'z' }, { data: "bryan", level: 'z' }, { data: "sam", level: 'o' }, { data: "nick", level: 'k' }, { data: "angel", level: 'l' }, { data: "arla", level: 'z' }, { data: "marley", level: 'o' }],
        order = Object.fromEntries([...sortPriority].map((c, i) => [c, i + 1]));
    
    order.default = Number.MAX_VALUE;
    
    data.sort((a, b) =>
        (order[a.level] || order.default) - (order[b.level] || order.default) ||
        a.data.localeCompare(b.data)
    );
    
    console.log(data);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  2. const sortPriority = "ramoklyxq";
    
    const items = [
        {data: "aizak"  , level: 'z'},
        {data: "alice"  , level: 'r'},
        {data: "bob"    , level: 'a'},
        {data: "trinity", level: 'm'},
        {data: "ella"   , level: 'z'},
        {data: "John"   , level: 'z'},
        {data: "bryan"  , level: 'z'},
        {data: "sam"    , level: 'o'},
        {data: "nick"   , level: 'k'},
        {data: "angel"  , level: 'l'},
        {data: "arla"   , level: 'z'},
        {data: "marley" , level: 'o'}
    ];
    
    ////
    
    const priorityMap = Array.from( sortPriority ).reduce( ( m, ch, idx ) => { m.set( ch, idx + 1 ); return m; }, new Map() );
    
    const output = items
      .map( o => ({ o, order: priorityMap.get( o.level ) || Number.MAX_SAFE_INTEGER }) )
      .sort( ( x, y ) => ( x.order - y.order ) || ( x.o.data.localeCompare( y.o.data ) ) )
      .map( i => i.o )
    ;
    
    console.log( output );
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search