简体   繁体   English

排序选择选项元素

[英]sort Select Options elements

I'm having an issue with sorting my select list elements.. In fact I have two dependent lists one with a list of "models" which I have sorted successfully using this function:我在排序我的选择列表元素时遇到问题.. 事实上,我有两个依赖列表,一个带有我使用此功能成功排序的“模型”列表:

var sel = $('#model');
var opts_list = sel.find('option');
opts_list.sort(function(a, b) { return $(a).text() > $(b).text() ? 1 : -1; });
sel.html('').append(opts_list);

But the second list contains the versions of the chosen model with the build number between brackets...但是第二个列表包含所选模型的版本,括号之间是内部版本号......

Examples:例子:

 2.00.312 (11)  
 5.105.402 (43)
 2.05.105 (141)
 4.54.200 (151)
 2.35.504 (61)
 1.204.454 (10)

http://jsfiddle.net/e9hdaqpv/ http://jsfiddle.net/e9hdaqpv/

a regex made the job:一个正则表达式完成了这项工作:

FIDDLE小提琴

items[value].sort(function (a, b) {
    a = Object.keys(a)[0].replace(/\(([^)]+)\)/,"").replace(/\./g,"");
    b = Object.keys(b)[0].replace(/\(([^)]+)\)/,"").replace(/\./g,"");
    console.log(a,b);
    return a > b ? 1 : -1;
});

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

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