-1

I'm not sure I am asking the right question or not. I get a JSON response like this from my server request

{
"20150917":
     {
         "Daily01sec":
            {
                "TG01152600000": "\/20150917\/Daily01sec\/TG0115260.bin",
                "TG01152600600": "\/20150917\/Daily01sec\/TG0115260.bin",
            }
        },
"201510":
        {
            "05":
            {
                "Daily01sec":
                {
                    "TG01152780600": "\/201510\/05\/Daily01sec\/TG01152780600.bin",                       
                }
            }
        },
"201509":
        {
            "05":
            {
                "Daily01sec":
                {
                    "TG01152780600": "\/201510\/05\/Daily01sec\/TG01152780600.bin",                       
                }
            }
        }
}

I want to count all folders and sort them.

I want to get sorted like this for master folders:

"201509"
"201510"
"20150817"

please check the example data at here http://jsfiddle.net/ebrahimmm/ncykb0qq/1/

here is the code receiving via $http at angularJS

angular.module('app',[])
    .controller('cntl',function($scope,$http){
    $http.get('http://'+'server' + "/getdatalogtree").
    then(
       function (response) {
        var data=response.data;
       //here i need sorting     
       },function(response){
    });
});

I also want to put each mast folders and its content to be an element of an array.

Ebrahim
  • 1,740
  • 2
  • 25
  • 31

3 Answers3

1

You can use Object.keys(Your_JSON_Response) method, to get array of keys. And then Array.sort() method...

Leonid Lazaryev
  • 326
  • 1
  • 8
1

Edit :: One-Liner

This should do the job :

function foo(dataString) {
  return Object.keys(JSON.parse(dataString)).map(parseFloat).sort(function(a,b) {return a[0]-b[0]}).map(String); //datastring -> Your JSON string which you get from the server
}
Dhruv Ramani
  • 2,633
  • 2
  • 22
  • 29
0

This one line will do this job:

function foo(dataString) {
    return Object.keys(dataString).sort();}
Divyesh Puri
  • 196
  • 8