Question: I have an js object like

```
{
  a: 1,
  b: 2,
  c: 3
}

```

I wanted to stringify the above object using JSON.stringify with the same order. That means, the stringify should return me the strings as below,

```
"{"a":"1", "b":"2", "c":"3"}"

```

But it is returning me like the below one if my js object has too many properties say more than 500,

```
"{"b":"2", "a":"1", "c":"3"}"

```

Is there any option to get my js object's json string as in sorted in asc.

Answer: 
If the order is important for you, don't use `JSON.stringify` because the order is not safe using it, you can create your JSON stringify using javascript, to deal with string values we have 2 different ways, first to do it using regexp an replace invalid characters or using `JSON.stringify` for our values, for instance if we have a string like `'abc\d"efg'`, we can simply get the proper result `JSON.stringify('abc\d"efg')`, because the whole idea of this function is to stringify in a right order:

```
function sort_stringify(obj){
    var sortedKeys = Object.keys(obj).sort();
    var arr = [];
    for(var i=0;i<sortedKeys.length;i++){
        var key = sortedKeys[i];
        var value = obj[key];
        key = JSON.stringify(key);
        value = JSON.stringify(value);
        arr.push(key + ':' + value);
    }
    return "{" + arr.join(",\n\r") + "}";
}
var jsonString = sort_stringify(yourObj);

```

If we wanted to do this not using `JSON.stringify` to parse the keys and values, the solution would be like:

```
function sort_stringify(obj){
    var sortedKeys = Object.keys(obj).sort();
    var arr = [];
    for(var i=0;i<sortedKeys.length;i++){
        var key = sortedKeys[i];
        var value = obj[key];
        key = key.replace(/"/g, '\\"');
        if(typeof value != "object")
            value = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
        arr.push('"' + key + '":"' + value + '"');
    }
    return "{" + arr.join(",\n\r") + "}";
}

```
The JavaScript objects are unordered by definition (you may refer to ECMAScript Language Specification under section 8.6, [click here for details](http://www.ecma-international.org/publications/standards/Ecma-262.htm) ). 

The language specification doesn't even guarantee that, if you iterate over the properties of an object twice in succession, they'll come out in the same order the second time.

If you still required sorting, convert the object into Array apply any sorting algorithm on it and then do JSON.stringify() on sorted array.

Lets have an example below as:

```
var data = {
    one: {
        rank: 5
    },
    two: {
        rank: 2
    },
    three: {
        rank: 8
    }
};

var arr = [];

```

Push into array and apply sort on it as :

```
var mappedHash = Object.keys( data ).sort(function( a, b ) {
    return data[ a ].rank - data[ b ].rank;
}).map(function( sortedKey ) {
    return data[ sortedKey ];
});

```

And then apply JSON.stringy :

```
var expectedJSON = JSON.stringify(mappedHash);

```

The output will be:

```
"[{"rank":2},{"rank":5},{"rank":8}]"

```