简体   繁体   English

如何使用js中的符号对列表进行排序

[英]How to sort a list with symbols in js

I have a drop down with a list of options which looks like this: 我有一个下拉列表,其中包含一个如下所示的选项列表:

<select id="weightClass" class="form-control input-sm">
     <option value="Default">Please Select</option>
     <option>200</option>
     <option>&lt;200</option>
     <option>&gt;200</option>
     <option>Unknown</option>
</select>

I'm trying to sort them in a custom order so it looks like this: 我正在尝试按自定义顺序对它们进行排序,所以它看起来像这样:

<select id="weightClass" class="form-control input-sm">
     <option value="Default">Please Select</option>
     <option> <200 </option>
     <option> 200 </option>
     <option> >200 </option>
     <option>Unknown</option>
</select>

Basically arranging it so that the less than option comes first, then the standard, then greater than, then unknown. 基本上安排它,以便小于选项首先,然后标准,然后大于,然后未知。

This is dynamically generated, the values could be anything, but there is always 4, and always in that format. 这是动态生成的,值可以是任何值,但始终为4,并始终采用该格式。 Always including an unknown, >, < and standard value. 始终包含未知的,>,<和标准值。

My current code is: 我目前的代码是:

function sortDropDown(target) {
    $(target).html($(target + " option").sort(function (a, b) {
        return a.text === b.text ? 0 : a.text < b.text ? -1 : 1;
    }));
}

Which obviously doesnt work, any ideas how i could get around this, can't seem to find anything about sorting with > < symbols included. 这显然不起作用,任何想法如何解决这个问题似乎无法找到任何关于> <符号包括的排序。

Extract the text (exclude the 1st one), sort and swap out the text using the sorted array 提取文本(排除第一个),使用已排序的数组对文本进行排序和交换

With HTML 使用HTML

<select id="weightClass" class="form-control input-sm">
    <option value="Default">Please Select</option>
    <option>&lt;200</option>
    <option>&gt;200</option>
    <option>200</option>
    <option>Unknown</option>
</select>

you can use this script ( after the above HTML) 你可以使用这个脚本( 上面的HTML之后)

var sortOrder = {
    '<': 1,
    // placeholder for the value
    '>': 3,
    'U': 4
}

function sortDropDown(target) {
    // get text
    var opts = [];
    $(target + " option").each(function (i) {
        if (i)
            opts.push($(this).text())
    });

    opts.sort(function (a, b) {
        // get the sort order using the first character
        var oa = sortOrder[a[0]] || 2;
        var ob = sortOrder[b[0]] || 2;
        if (oa > ob)
            return 1;
        else if (oa < ob)
            return -1;
        else
            return 0;
    });

    // change the option text
    $(target + " option").each(function (i) {
        if (i)
            $(this).text(opts[i - 1])
    });
}

sortDropDown("#weightClass")

 var sortOrder = { '<': 1, '>': 3, 'U': 4 } function sortDropDown(target) { var opts = []; $(target + " option").each(function (i) { if (i) opts.push($(this).text()) }); console.log(opts) opts.sort(function (a, b) { var oa = sortOrder[a[0]] || 2; var ob = sortOrder[b[0]] || 2; if (oa > ob) return 1; else if (oa < ob) return -1; else return 0; }); $(target + " option").each(function (i) { if (i) $(this).text(opts[i - 1]) }); } sortDropDown("#weightClass") 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="weightClass" class="form-control input-sm"> <option value="Default">Please Select</option> <option>&lt;200</option> <option>&gt;200</option> <option>200</option> <option>Unknown</option> </select> 

You should use a sorting function to do the special sort that you need. 您应该使用排序功能来执行所需的特殊排序。
Here is the sorting function with an example of how to use it: 这是排序函数,以及如何使用它的示例:

var t = ['some other option', '100', '120', '300', '>300', '<300', '<100', '>400', '200', '<200', '>200', 'another special option']
function specialSort(a, b) {
    var newA, newB;
    if (a[0] == '<' || a[0] == '>') {
        newA = parseInt(a.substr(1))
    } else if (!isNaN(a)) {
        newA = parseInt(a)
    } else {
        newA = null;
    }

    if (b[0] == '<' || b[0] == '>') {
        newB = parseInt(b.substr(1))
    } else if (!isNaN(b)) {
        newB = parseInt(b)
    } else {
        newB = null;
    }

    if (newA == null) {
        return 1;
    }
    if (newB == null) {
        return -1;
    }

    if (typeof(newA) == 'number' && typeof(newB) == 'number') {
        if (newA < newB) {
            return -1;
        } else if (newA > newB) {
            return 1;
        } else if (newA == newB) {
            if (a[0] == '<') {
                return -1;
            } else if (b[0] == '<') {
                return 1;
            } else if (a[0] == '>') {
                return 1
            } else if (b[0] == '>' ) {
                return -1;
            }
        }
    }
    return 0;
}
console.log(t.sort(specialSort));
// Output is ["<100", "100", "120", "<200", "200", ">200", "<300", "300", ">300", ">400", "another special option", "some other option"]

You can use jQuery to get the values of your SELECT tag and sort them using this: 您可以使用jQuery获取SELECT标记的值并使用以下方法对它们进行排序:

valuesToSort = []
$('#weightClass').find('option').each(function(){r.push($(this).text())})
valuesToSort.sort(specialSort)

Now the array valuesToSort is sorted the way you want and you can put the values back into your #weightClass tag. 现在,数组valuesToSort按您希望的方式排序,您可以将值放回到#weightClass标记中。

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

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