具有下一个数组(例如):
<?xml version="1.0" encoding="UTF-8"?>
<messageProcessor class="org.apache.synapse.message.processor.impl.sampler.SamplingProcessor" messageStore="AmqpConnection" name="AmqpProcessor" xmlns="http://ws.apache.org/ns/synapse">
<parameter name="cronExpression">0 */15* ? * *</parameter>
<parameter name="sequence">AmqpTest</parameter>
<parameter name="interval">1000</parameter>
<parameter name="is.active">true</parameter>
<parameter name="concurrency">1</parameter>
</messageProcessor>
我需要对其进行排序以得到下一个:
[
['Tue', 'Fri'],
['Mon'],
['Mon', 'Thu', 'Sun'],
['Wed', 'Thu', 'Sun'],
['Tue', 'Wed']
]
并不是说数组中可以有多少天-如果多于1,则应将其他人分别按星期的位置索引进行排序。
如何实现这种分类功能?谢谢。
答案 0 :(得分:3)
这并不会考虑所有情况,而是执行以下操作:
_a
或_b
)默认为0
,因为缺少元素是更高的优先级
let order = {
'Mon':1,
'Tue':2,
'Wed':3,
'Thu':4,
'Fri':5,
'Sat':6,
'Sun':7
}
let arr = [
['Tue', 'Fri'],
['Mon'],
['Mon', 'Thu', 'Sun'],
['Wed', 'Thu', 'Sun'],
['Tue', 'Wed']
]
let sorted = arr.sort(sortElements)
console.log('sorted:',sorted)
function sortElements(a,b,ndx=0){
let _a = order[ a[ndx] ] || 0
let _b = order[ b[ndx] ] || 0
if ( _a === _b && (a.length > ndx || b.length > ndx))
return sortElements(a,b,ndx+1)
else
return _a - _b
}
答案 1 :(得分:0)
如果您可以重组数据,这可能会有所帮助。
let days = [
{day:'Mon',index:1},
{day:'Sat',index:6},
{day:'Sun',index:0},
{day:'Wed',index:3},
{day:'Tue',index:2},
{day:'Thu',index:4},
{day:'Fri',index:5},
];
days.sort((a,b) => {return a.index > b.index});
答案 2 :(得分:0)
我编写了一个compareArray函数,用于确定两个数组的顺序,例如['Tue','Wed']和['Tue','Fri']。然后在排序功能中使用它
var arr = [
['Tue', 'Fri'],
['Mon'],
['Mon', 'Thu', 'Sun'],
['Wed', 'Thu', 'Sun'],
['Tue', 'Wed']
]
var order = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
var compareArray = (arr1, arr2) => {
for(var i=0;i<arr1.length;i++){
if(!arr2[i]) {return 1}
if(order.indexOf(arr1[i]) < order.indexOf(arr2[i])) {
return -1;
} else if(order.indexOf(arr1[i]) > order.indexOf(arr2[i])){
return 1;
}
}
return 0;
}
arr.sort((a,b) => compareArray(a,b))
console.log(arr)
答案 3 :(得分:0)
您可以带一个带有可排序项目的对象,该对象可以堆放在字符串中。
const
days = { Mon: 'a', Tue: 'b', Wed: 'c', Thu: 'd', Fri: 'e', Sat: 'f', Sun: 'g' },
getV = a => a.map(d => days[d]).join('');
var array = [['Tue', 'Fri'], ['Mon'], ['Mon', 'Thu', 'Sun'], ['Wed', 'Thu', 'Sun'], ['Tue', 'Wed']];
array.sort((a, b) => getV(a).localeCompare(getV(b)));
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 4 :(得分:0)
如果编写两个函数来将名称转换为值并将值转换为名称,则它是单行的。
function dayvalue (day)
{
switch (day) {
case 'Mon': return 0;
case 'Tue': return 1;
case 'Wed': return 2;
case 'Thu': return 3;
case 'Fri': return 4;
case 'Sat': return 5;
case 'Sun': return 6;
}
}
function dayname (value)
{
switch (value) {
case 0: return 'Mon';
case 1: return 'Tue';
case 2: return 'Wed';
case 3: return 'Thu';
case 4: return 'Fri';
case 5: return 'Sat';
case 6: return 'Sun';
}
}
var days = [
['Tue', 'Fri'],
['Mon'],
['Mon', 'Thu', 'Sun'],
['Wed', 'Thu', 'Sun'],
['Tue', 'Wed']
];
console.log(days.map(names => names.map(dayvalue)).sort().map(values => values.map(dayname)));
答案 5 :(得分:0)
您可以通过将每个数组转换为字符串表示形式并使用String.prototype.localeCompare()
进行排序来实现:
const input = [['Tue', 'Fri'],['Mon'],['Mon', 'Thu', 'Sun'],['Wed', 'Thu', 'Sun'],['Tue', 'Wed']];
const days = {Mon:'a', Tue:'b', Wed:'c', Thu:'d', Fri:'e', Sat:'f', Sun:'g'};
const sorted = input.sort((a,b) => {
const [aLoc, bLoc] = [a,b].map(e => e.map(d => days[d]).join(''));
return aLoc.localeCompare(bLoc);
});
console.log(sorted);