Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an array() follow a rule on sorting elements

The array can have different size but max it could contain the following elements:

DE/FR/IT/EN/RM

some of the possible arrays could be:

DE/FR/IT/EN/RM,

IT/EN/RM,

DE/RM

etc.

How can I make an array like that follow a sorting rule? By meaning how can I make that array always sort itself following this order: DE/FR/IT/EN/RM

I tried something like this, but as not being a js coder I cant make any sense out of it:

function{
.....
....
...
list.sort(compareList());
...
..
.
}

function compareList() {
    var ruleList = new Array();
    ruleList.push("DE","FR","IT","EN","RM");


}

E.g.

input array with 3 elements: RM,DE,EN

to output after sorting: DE,EN,RM

OR e.g

input with the maxed 5 elements: FR,DE,EN,RM,IT

to output:DE,FR,IT,EN,RM

like image 455
Sir. Hedgehog Avatar asked Mar 06 '23 04:03

Sir. Hedgehog


2 Answers

You could take an object for the wanted order and a default value for not known value and sort by the delta of the values. Not known items are sorted to the end of the array.

function compareList() {
    var order = { DE: 1, FR: 2, IT: 3, EN: 4, RM: 5, default: Infinity };
    return (a, b) => (order[a] || order.default) - (order[b] || order.default);
}

console.log(['RM', 'DE', 'EN'].sort(compareList()));             // DE EN RM
console.log(['FR', 'DE', 'EN', 'RM', 'IT'].sort(compareList())); // DE FR IT EN RM
.as-console-wrapper { max-height: 100% !important; top: 0; }

For keeping the first element, you could give that value a great negative value for sorting. This works only by knowing the array in advance, because on later sorting, there is no information about an index.

const
    keepFirst = (array, sort) => {
        var order = array.reduce((r, v, i) => (r[v] = i + 1, r), { default: Infinity });
        order[array[0]] = -Infinity;
        return array.sort((a, b) => (order[a] || order.default) - (order[b] || order.default));
    },
    order = ['DE', 'FR', 'IT', 'EN', 'RM'];

console.log(keepFirst(['RM', 'DE', 'EN'], order).join(' '));             // RM DE EN
console.log(keepFirst(['FR', 'DE', 'EN', 'RM', 'IT'], order).join(' ')); // FR DE IT EN RM
like image 193
Nina Scholz Avatar answered Mar 15 '23 22:03

Nina Scholz


var base =  ["DE","FR","IT","EN","RM"];
var test = ["DE","IT","FR","EN"];
test.sort((a,b) => base.indexOf(a) - base.indexOf(b));
console.log(test);

You can use the index of the base array as the order

var base =  ["DE","FR","IT","EN","RM"];
var test = ["DE","IT","FR","EN"];
test.sort((a,b) => base.indexOf(a) - base.indexOf(b));
console.log(test);
like image 21
Vineesh Avatar answered Mar 15 '23 21:03

Vineesh