Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在dropdownlist中按字母顺序对数组排序_Javascript_Sorting - Fatal编程技术网

Javascript 如何在dropdownlist中按字母顺序对数组排序

Javascript 如何在dropdownlist中按字母顺序对数组排序,javascript,sorting,Javascript,Sorting,我正在尝试使用fixes的sort()函数对下一个dropdownlist进行排序,但它对我不起作用 下面是我尝试使用加载数据的javascript函数按字母顺序排序的dropdownlist 函数加载服务(){ const url=document.getElementById(“服务”).value; $.ajax({ url:url, 类型:“POST”, 数据类型:“json”, 成功:功能(数据){ 如果(data.length>0){ const service=document.

我正在尝试使用fixes的sort()函数对下一个dropdownlist进行排序,但它对我不起作用

下面是我尝试使用加载数据的javascript函数按字母顺序排序的dropdownlist

函数加载服务(){

const url=document.getElementById(“服务”).value; $.ajax({ url:url, 类型:“POST”, 数据类型:“json”, 成功:功能(数据){ 如果(data.length>0){ const service=document.getElementById(“ticketServiceInput”); const arrDataSorted=data.sort((a,b)=>(a.value>b.value)?1:((b.value>a.value)?-1:0); for(让idx进入arrDataSorted){ if(arrDataSorted.hasOwnProperty(idx)){ const option=document.createElement(“选项”); option.innerHTML=arrDataSorted[idx].Name; option.value=arrDataSorted[idx].ServiceId; service.options.add(选项); } } } } }); };

服务:
选择一个选项
为什么不用于此。又好又简单。鉴于您共享的数据:

    arrDataSorted = _.sortBy(data, (x) => {
      return x.Name;
    });
或者使用您的功能:

 let arrDataSorted = data.sort(function(a, b) {
        var nameA = a.Name.toUpperCase(); // ignore upper and lowercase
        var nameB = b.Name.toUpperCase(); // ignore upper and lowercase
        if (nameA < nameB) {
          return -1;
        }
        if (nameA > nameB) {
          return 1;
        }

        // names must be equal
        return 0;
    });
让arrDataSorted=data.sort(函数(a,b){
var nameA=a.Name.toUpperCase();//忽略大小写
var nameB=b.Name.toUpperCase();//忽略大小写
if(nameA名称B){
返回1;
}
//名字必须相等
返回0;
});
为什么不用于此。又好又简单。鉴于您共享的数据:

    arrDataSorted = _.sortBy(data, (x) => {
      return x.Name;
    });
或者使用您的功能:

 let arrDataSorted = data.sort(function(a, b) {
        var nameA = a.Name.toUpperCase(); // ignore upper and lowercase
        var nameB = b.Name.toUpperCase(); // ignore upper and lowercase
        if (nameA < nameB) {
          return -1;
        }
        if (nameA > nameB) {
          return 1;
        }

        // names must be equal
        return 0;
    });
让arrDataSorted=data.sort(函数(a,b){
var nameA=a.Name.toUpperCase();//忽略大小写
var nameB=b.Name.toUpperCase();//忽略大小写
if(nameA名称B){
返回1;
}
//名字必须相等
返回0;
});

原来答案是改变

const arrDataSorted = data.sort((a, b) => (a.value > b.value) ? 1 : ((b.value > a.value) ? -1 : 0));

在Sebastian提供了他的示例数据之后

[{"ServiceId":1,"Name":"BLACKBOARD","Content":"Software de educación"}...]
很明显,他想根据对象的
名称
属性排序,而不是


Chev在另一个回答中建议使用
lodash
来帮助实现这些排序功能,我完全同意这一点。在使用JS时,Lodash是一个很好的工具。

事实证明,答案是改变

const arrDataSorted = data.sort((a, b) => (a.value > b.value) ? 1 : ((b.value > a.value) ? -1 : 0));

在Sebastian提供了他的示例数据之后

[{"ServiceId":1,"Name":"BLACKBOARD","Content":"Software de educación"}...]
很明显,他想根据对象的
名称
属性排序,而不是


Chev在另一个回答中建议使用
lodash
来帮助实现这些排序功能,我完全同意这一点。使用JS时,Lodash是一个很好的工具。

数据看起来像什么我从数据库中获取数据数据库中的数据是否有以数字开头的字符串?Javascript排序可能有问题。const url=document.getElementById(“服务”).value;但是链接没有id?@LeeSalminen这是返回
[{“ServiceId”:1,“Name”:“BLACKBOARD”,“Content”:“Software de educación,
数据看起来像什么我从数据库中获取数据数据库中的数据是否有以数字开头的字符串?Javascript排序可能会有问题。const url=document.getElementById(“服务”).value;但链接没有id?@LeeSalminen这是返回
[{“ServiceId”:1,“Name”:“BLACKBOARD”,“Content”:“Software de educacón,