比较javascript或jquery中的单词后对列表值进行排序
我想在与一个单词比较后对列表标记进行排序,比如如果多个语句中有单词“one”,那么它应该首先进行排序,然后检查其他语句中的其他单词,比如剩余语句中的“two”,这样具有“two”单词的语句应该位于包含“one”的语句之后。所以请大家帮我写逻辑。看一看我的代码,我只用于按字母顺序排序(基于以下代码中给出的状态) 示例:比如比较javascript或jquery中的单词后对列表值进行排序,javascript,jquery,sorting,Javascript,Jquery,Sorting,我想在与一个单词比较后对列表标记进行排序,比如如果多个语句中有单词“one”,那么它应该首先进行排序,然后检查其他语句中的其他单词,比如剩余语句中的“two”,这样具有“two”单词的语句应该位于包含“one”的语句之后。所以请大家帮我写逻辑。看一看我的代码,我只用于按字母顺序排序(基于以下代码中给出的状态) 示例:比如manoj kumarrohit koulsachin kumar所以在这三个列表中,我想先对包含“kumar”字符串的人进行排序,然后依次类推 <script id="t
- manoj kumar
- rohit koul
- sachin kumar
<script id="template" type="text/html">
<ul class="checklist" data-role="listview" data-inset="true" data-autodividers="true" id="mylist">
{{#container}}
{{#nid}}<li><a href="checklist-detail.html?nid={{nid}}">{{name}} - {{status}} {{#date}}<br/>
<span class="due-date">{{date}}</span>{{/date}}</a></li>{{/nid}}
{{/container}}
</ul>
</script>
look once my javascript code but this is not for my desire code it is only
for sort alphabetically (help me in this code that how to i write
condition or logic for sort according to particular word comparison)
var mylist = $('ul');
var listitems = mylist.children('li').get();
listitems.sort(function(a, b) {
var compA = $(a).text().toUpperCase();
var compB = $(b).text().toUpperCase();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
});
$.each(listitems, function(idx, itm) { mylist.append(itm); });
{{{#容器}
{{{nid}- {/li>{{/nid}}
{{/container}}
看一下我的javascript代码,但这不是我想要的代码,它只是
对于按字母顺序排序(请帮助我在这段代码,我如何写
根据特定单词比较进行排序的条件或逻辑)
var mylist=$('ul');
var listitems=mylist.children('li').get();
排序(函数(a,b){
var compA=$(a.text().toUpperCase();
var compB=$(b.text().toUpperCase();
返回(compAcompB)?1:0;
});
$.each(列表项,函数(idx,itm){mylist.append(itm);});
您可以根据标签是否包含预定义的有序单词列表中的单词来给标签打分。例如:
var ordered_words = ["kumar", "koul", "simon"]
var tags = ["manoj kumar", "rohit koul", "sachin kumar", "simon f (me)"]
var scored_tags = []
var i = 0, tag
while (tag = tags[i]) {
var j = 0, word, found = false
while (found == false && j < ordered_words.length) {
word = ordered_words[j]
if (tag.indexOf(word) != -1) {
scored_tags.push({tag: tag, score: j})
found = true
}
j++
}
i++
}
console.log(scored_tags)
然后,您可以使用score参数对该数组进行排序,如下所示:
scored_tags.sort(function(a, b) {
var compA = a.score
var compB = b.score
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
});
scored_标记。排序(函数(a,b){
var compA=a.分数
var compB=b.分数
返回(compAcompB)?1:0;
});
下面是供您使用的代码:
你可以添加一些你想要比较的文本的例子吗?为你的代码创建提琴例子:比如说:像
- manoj kumar
- rohit koul
- sachin kumar
scored_tags.sort(function(a, b) {
var compA = a.score
var compB = b.score
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
});