Sort JSON alphabetically

前端 未结 5 1121
我寻月下人不归
我寻月下人不归 2021-01-02 13:08

I have a JSON object generated based on data stored in a table. I then need to be able to sort it in different ways, but when I do JSON.stringify(array) and try

相关标签:
5条回答
  • 2021-01-02 13:14

    The Mozilla developer documentation has a helpful explanation of the sort function. You can provide a function as a parameter that tells the browser how to do the sort.

    https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

    Some pseudo-code from that link, with the myArray.sort call added and the function name removed:

    myArray.sort(function(a, b) {
      if (a is less than b by some ordering criterion)
         return -1;
      if (a is greater than b by the ordering criterion)
         return 1;
      // a must be equal to b
      return 0;
    });
    

    EDIT: It looks like you have a single-element array with an object as the element. Objects are unordered. You have to transform it to an array before it can be sorted. Try this (with the Object.keys and Array.prototype.map functions added via augment.js for older browsers):

    var result = Object.keys(myArray[0]).sort().map(function(key) {
        return [key,myArray[0][key]];
    });
    

    This will give you a sorted, nested array of the form:

    [["Brand Name","threadless"],
     ["Function Category","T-Shirt"],
     ...
    ]
    
    0 讨论(0)
  • 2021-01-02 13:23

    Something of this kind might be on help:

    function (obj) {
       var a = [],x;
       for(x in obj){ 
         if(obj.hasOwnProperty(x)){
             a.push([x,obj[x]]);
         }
       }
       a.sort(function(a,b){ return a[0]>b[0]?1:-1; })
       return a;
    }
    
    
    0 讨论(0)
  • 2021-01-02 13:24

    First, define a compare function:

    function compare(el1, el2, index) {
      return el1[index] == el2[index] ? 0 : (el1[index] < el2[index] ? -1 : 1);
    }
    

    Then, to sort the array on the first column, use this:

    array.sort(function(el1,el2){
      return compare(el1, el2, "Functional Category")
    });
    

    Or, to sort on the first column A-Z, and on the second column Z-A if the first column is equal:

    array.sort(function(el1,el2){
      var compared = compare(el1, el2, "Functional Category")
      return compared == 0 ? -compare(el1, el2, "Brand Name") : compared;
    });
    
    0 讨论(0)
  • 2021-01-02 13:38

    see below code ....

    function sortObject(o) {
        var sorted = {},
        key, a = [];
    
        for (key in o) {
            if (o.hasOwnProperty(key)) {
                    a.push(key);
            }
        }
    
        a.sort();
    
        for (key = 0; key < a.length; key++) {
            sorted[a[key]] = o[a[key]];
        }
        return sorted;
    }
    

    Does that help?

    0 讨论(0)
  • 2021-01-02 13:39

    If your intent is to allow the user to sort the table dynamically, then my approach may be slightly different.

    There are plenty of jQuery table sorting plugins. I have had good success with this one: http://tablesorter.com/docs/

    Allows multi-column sort, a default sort, wire up to events, etc... etc...

    This way you can build your table as you already are, and sorting can be done secondary even before page output.

    0 讨论(0)
提交回复
热议问题