如何使json数组排序函数不区分大小写? [英] how can I make my json array sort function case insensitive?

查看:129
本文介绍了如何使json数组排序函数不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试进行以下尝试-

  • 采用json数组
  • 按显示名称值对数组进行排序
  • 将排序后的数组作为列表项添加到DOM

我的问题是我希望能够进行排序,而在排序时不考虑值的大小写...我注意到它考虑了大小写,并将大写的值放在首位.

My issue is that I want to be able to make the sort NOT take into consideration the case of the value when sorting... Im noticing that it considers the case and puts uppercase values FIRST.

我相当确定这是在某处集成.toLowerCase()的问题,但我的所有尝试均以失败告终.在哪里/如何应用.toLowerCase()以使排序不区分大小写?

Im fairly certain its a matter of integrating .toLowerCase() somewhere but all my attempts have failed. Where / how do I apply .toLowerCase() to make the sort case insensitive?

js在这里摆弄

function sortResults(prop, asc) {
    //SORT THE ARRAY BY THE PASSED NODE VALUE...
          myArray.jsonData = myArray.jsonData.sort(function(a, b) {
                if (asc) return (a[prop]> b[prop]) ? 1 : ((a[prop] < b[prop]) ? -1 : 0);
                else return (b[prop] > a[prop]) ? 1 : ((b[prop] < a[prop]) ? -1 : 0);
          });

     showInDOM();   
 }

推荐答案

使用函数toLowerCase()并使用+ ""

Use the function toLowerCase() and convert to string with + ""

myArray.jsonData.sort(function(a, b) {
   var comp = ((a[prop]+"").toLowerCase() >
               (b[prop]+"").toLowerCase()) ? 1 : -1 ;
   return asc ? comp : -comp;
});

这篇关于如何使json数组排序函数不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆