how can I make my json array sort function case insensitive?

后端 未结 2 332
渐次进展
渐次进展 2021-01-28 08:32

I have the following fiddle that is my attempt at

  • taking a json array
  • sort the array by the displayname value
  • add the sorted array to the DOM as
2条回答
  •  [愿得一人]
    2021-01-28 09:04

    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;
    });
    

提交回复
热议问题