views:

812

answers:

2

i want to sort the keys or name of the method.. following is the object http://pastie.org/411854

A: 

To get a sorted array of the function names you could use the following:

var functions = [];
for(functionName in apiDocs) {
  functions.push(functionName);
}
functions.sort();

If you want to perform some sort of more complex sort you could do something along the lines of

var functions = [];
for(functionName in apiDocs) {
  functions.push(apiDocs[functionName]);
}
functions.sort(//put sorting closure here)

You can get more information about custom sorting here: Array Sorting Explained

ihumanable
+1  A: 

If I understand correctly, you would like to put members in order based on key or method name (which is the same value)? This has no meaning for Object since members have no order.

However, if you put these objects in an array like this:

var apiDocs = [
    {
     "methodName": "friendsGetByUser",
     "methodDescription": "Returns user ids of friends of the specified user.",
     ...
    }, {
     "methodName": "friendsGetBestFriends",
     "methodDescription": "Returns user ids of best friends of the logged in user.",
     ...
    }
    ...
];

Then you can sort the array easily by invoking Array.sort passing in compare function.

apiDocs.sort(function (a, b) {
     return a.methodName < b.methodName;
    });
Marko Dumic