按同名顺序,然后按字母顺序 [英] Order by same name and then alphabetical
问题描述
我有一个Мебельноепроизводство"值,并且我有一个表,其中包含所有与Мебельноепроизводство"巧合有关的项目.
I have a this value 'Мебельное производство' And I have a table which contains all the related items that has 'Мебельное производство' as a coincidence.
到目前为止,我已按字母顺序对它进行排序,但我需要在表上返回所有与值Мебельноепроизводство"相符的巧合,而其余的均应按字母顺序返回.
So far I sort it in Alphabetical way, but I need to return on the table all the coincidence with the value 'Мебельное производство' and the rest on alphabetical way.
到目前为止,我被困在这里:
So far I'm stucked here:
jQuery(".orderings tr").sort(sort).appendTo('.orderings');
function sort(a, b){
return (jQuery(b).text()) < (jQuery(a).text()) ? 1 : -1;
}
有什么想法吗?
推荐答案
您需要先进行比较,然后进行alpha排序
You need to do the first comparison, then the alpha sorting
尽管如此,该匹配项仍未排序:
This one does not sort the matches though:
jQuery(".orderings tr").sort(sort).appendTo('.orderings');
var match = "Мебельное производство";
function sort(a, b) {
var at = jQuery(a).text();
var bt = jQuery(b).text();
if (at.indexOf(match) >= 0)
{
return -1; // force to the top
}
return (bt < at) ? 1 : -1;
}
这也对匹配项进行了初始排序:
This one also does the initial sorting of the matches:
function sort(a, b) {
var at = jQuery(a).text();
var bt = jQuery(b).text();
console.log(at.indexOf(match));
if (at.indexOf(match) >= 0 && bt.indexOf(match) < 0) {
return -1;
} else if (bt.indexOf(match) >= 0 && at.indexOf(match) < 0) {
return 1;
}
return (bt < at) ? 1 : -1;
}
var match = "Мебельное производство";
jQuery(".orderings tr").sort(sort).appendTo('.orderings');
JSFiddle:: http://jsfiddle.net/s7nb5o6g/5/
注意:由于排序顺序的副作用,因此不需要进行else if
测试,但保留它不会有任何危害.例如 http://jsfiddle.net/s7nb5o6g/6/仍然有效
Note: the else if
test should not be required, due to a side-effect of the sort order, but it does no harm to leave it. e.g. http://jsfiddle.net/s7nb5o6g/6/ still works
这篇关于按同名顺序,然后按字母顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!