[英]Sorting by every nth element in javascript
I have a set of <tr>
elements that I'd like to sort in batches ie every nth element is used for comparison in the sorting algorithm and subsequent n-1 elements are moved along with the nth element. 我有一组<tr>
元素,我想分批排序,即每个第n个元素用于排序算法中的比较,随后的n-1个元素与第n个元素一起移动。 ( Update : I should clarify that I do not have control over the HTML) ( 更新 :我应该澄清我无法控制HTML)
In the following case, the rows are sorted alphabetically by the value of 1st, 4th & 7th element ie Apple, Banana & Coconut. 在下面的例子中,行按照第1,第4和第7个元素的值按字母顺序排序,即Apple,Banana和Coconut。 The subsequent 2 <tr>
elements following them are moved along with them ie batches of 3. 随后的2个<tr>
元素随之移动,即3个批次。
Before sorting: 排序前:
<tr> Banana </tr>
<tr> - Shake </tr>
<tr> - Chips </tr>
<tr> Apple </tr>
<tr> - Juice </tr>
<tr> - Sauce </tr>
<tr> Coconut </tr>
<tr> - Curry </tr>
<tr> - Water </tr>
After sorting: 排序后:
<tr> Apple </tr>
<tr> - Juice </tr>
<tr> - Sauce </tr>
<tr> Banana </tr>
<tr> - Shake </tr>
<tr> - Chips </tr>
<tr> Coconut </tr>
<tr> - Curry </tr>
<tr> - Water </tr>
There are some straight forward ways eg Split this into 3 lists and sort the first one normally. 有一些直接的方法,例如将其拆分为3个列表并正常排序第一个。 Then sort second & third based on first list. 然后根据第一个列表排序第二个和第三个。 My question is, is it possible to do it in the compare function in array.sort([compareFunction])
? 我的问题是,是否可以在array.sort([compareFunction])
的compare函数中array.sort([compareFunction])
?
Update : Another method would be to convert it into an array like this 更新 :另一种方法是将其转换为这样的数组
[['Banana','- Shake', '- Chips'],['Apple', '- Juice','- Sauce'],['Coconut','- Curry', '- Water']]
and sort it like this sort(function(a, b) {return a[0] - b[0]})
并按此sort(function(a, b) {return a[0] - b[0]})
I've used a basic array to display the method, but this worked for me: 我使用了一个基本数组来显示方法,但这对我有用:
var groupSize = 3;
var arr = ['banana',' - shake',' - chips','apple',' - juice',' - sauce','coconut',' - curry',' - water'];
var newArr = [];
while((sec = arr.splice(0,groupSize)).length > 0)
{
newArr.push(sec);
}
newArr.sort()
arr = [];
for(var i in newArr)
{
for(var j in newArr[i])
{
arr.push(newArr[i][j]);
}
}
console.log(arr); //["apple", " - juice", " - sauce", "banana", " - shake", " - chips", "coconut", " - curry", " - water"]
is it possible to do it in the compare function in
array.sort(compareFunction)
? 是否可以在array.sort(compareFunction)
的compare函数中array.sort(compareFunction)
?
Yes, by making the array
items hierarchy-aware. 是的,通过使array
项层次结构感知。 So you'd have to introduce a pointer from Shake
to Banana
, from Chips
to Banana
, from Juice
to Apple
and so on. 所以你必须引入一个从Shake
到Banana
,从Chips
到Banana
,从Juice
到Apple
等的指针。 The compare function would then look like 然后比较函数看起来像
function compare(a, b) {
var aTop = a.parent || a,
bTop = b.parent || b;
if (aTop.value > bTop.value) return 1;
if (aTop.value < aTop.value) return -1;
if (b.parent == a) return 1;
if (a.parent == b) return -1;
if (a.value > b.value) return 1;
if (a.value < b.value) return -1;
return 0;
}
It would be possible a bit more programmatically (with an array), but believe me - you don't really want this. 可能会以编程方式(使用数组),但相信我 - 你真的不想要这个。
A similar approach would be to to simply concatenate the arrays to strings ( ["Banana"]
→ "Banana"
, ["Banana", "Shake"]
→ "Banana-Shake"
) so that you didn't need a custom compare function any more, and then split them after sorting to get back the original values. 类似的方法是简单地将数组连接到字符串( ["Banana"]
→ "Banana"
, ["Banana", "Shake"]
→ "Banana-Shake"
),这样您就不需要自定义比较再次运行,然后在排序后拆分它们以获取原始值。
However, your grouping method is superior to these approaches as it requires much less comparisons. 但是,您的分组方法优于这些方法,因为它需要更少的比较。
I think you are going to require an extra pass through the table, this could be done when the table is created. 我认为你需要额外的通过表,这可以在创建表时完成。 You need to create a 'key' based on the header row plus the children. 您需要根据标题行和子项创建“键”。
So after this pass you have, i'm showing attributes but you could use js properties instead: 所以在你通过这个传递之后,我正在显示属性,但你可以使用js属性:
<tr data-key="Banana"> Banana </tr>
<tr data-key="Banana - Shake"> - Shake </tr>
<tr data-key="Banana - Chips"> - Chips </tr>
<tr data-key="Apple"> Apple </tr>
<tr data-key="Apple - Juice"> - Juice </tr>
<tr data-key="Apple - Sauce"> - Sauce </tr>
<tr data-key="Coconut"> Coconut </tr>
<tr data-key="Coconut - Curry"> - Curry </tr>
<tr data-key="Coconut - Water"> - Water </tr>
then sort by that key 然后按那个键排序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.