简体   繁体   English

如何使array()遵循排序元素的规则

[英]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 通过含义如何使该数组始终按照以下顺序排序: 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: 我尝试过类似的东西,但由于不是js编码器,我无法理解它:

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

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


}

Eg 例如

input array with 3 elements: RM,DE,EN 输入数组有3个元素:RM,DE,EN

to output after sorting: DE,EN,RM 排序后输出:DE,EN,RM

OR eg 或者例如

input with the maxed 5 elements: FR,DE,EN,RM,IT 输入最多5个元素:FR,DE,EN,RM,IT

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

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 

 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);

You could define a function like sortArrayBy() below to achieve this: 你可以在下面定义一个类似sortArrayBy()的函数来实现这个目的:

 // The sorting function, sorts input array by the by array function sortArrayBy(input, by) { var result = [].concat(input) result.sort(( str0, str1 ) => { const idx0 = by.indexOf(str0) const idx1 = by.indexOf(str1) // If str0 or str1 not found in by array, push these // to bottom of result if(idx0 === -1) return 1 if(idx1 === -1) return -1 return idx0 - idx1 }) return result; } var data = ['DE','FR','IT','EN','RM'] console.log('DE/FR/IT/EN/RM', sortArrayBy(data, ['DE','FR','IT','EN','RM'])) console.log('IT/EN/RM', sortArrayBy(data, ['IT','EN','RM'])) console.log('DE/RM', sortArrayBy(data, ['DE','RM'])) 

This sortArrayBy() method effectively takes an index array (ie by ) that is used to direct the sorting/ordering of input data (ie ['DE','FR','IT','EN','RM'] ) in the returned result array, 这个sortArrayBy()方法有效地获取了一个索引数组(即by ),用于指导input数据的排序/排序(即['DE','FR','IT','EN','RM'] )在返回的result数组中,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM