Generic way of sorting JSON array by attribute

后端 未结 3 1312
野的像风
野的像风 2020-12-07 21:32

I found out how to sort a JSON array at http://www.devcurry.com/2010/05/sorting-json-array.html

Now I want to sort it in a generic way; so that my sorting function k

3条回答
  •  醉酒成梦
    2020-12-07 21:54

    See this example http://jsfiddle.net/W8Byu/1/

    What I have done is stored the sort column Name in a variable and used in Sort function.

     var sortColumnName = "Name";
    
     function SortByName(x,y) {
          return ((x[sortColumnName]  == y[sortColumnName]) ? 0 : ((x[sortColumnName]>    y[sortColumnName]) ? 1 : -1 ));
        }
    

提交回复
热议问题