jsonQ.js
function(key, settings) {
            //merge global setting with local setting
            settings = jsonQ.merge({}, jsonQ.settings.sort, settings);
            var jobj = this.find(key),
                current = jobj.clone(),
                sortStack = [],
                i, ln,
                sortedPath = [],
                type = objType(jobj.pathValue(current[0].path)),
                //function to get value which is an array from pathKey traversing from right.
                getClosestArray = function(key) {
                    while (key.length !== 0) {
                        var lastKey = key.pop();
                        if (!isNaN(lastKey)) {
                            var val = jobj.pathValue(key);
                            if (objType(val) == 'array') {
                                return val;
                            }
                        }
                    }
                    return null;
                };

            //initialize sort stack
            for (i = 0, ln = current.length; i < ln; i++) {
                sortStack.push({
                    pathHolder: current[i].path.concat([]),
                    current: current[i].path.concat([])
                });
            }

            //to run the loop until all are sorted
            var alpha = 0,

                // function to remove element if sorting is done for that path
                spliceElm = function(i) {
                    sortStack.splice(i, 1);
                    return --i;
                };
            while (sortStack.length !== 0) {
                alpha++;
                for (i = 0; i < sortStack.length; i++) {
                    var cur = sortStack[i].current,
                        pH = sortStack[i].pathHolder,
                        //to get the closest array in the current path. This will also change value of current path variable.
                        ary = getClosestArray(cur),
                        pathStr = cur.join();



                    //to remove from sort stack if no array is left on key or if that is already sorted
                    if (cur.length === 0 || sortedPath.indexOf(pathStr) != -1) {
                        i = spliceElm(i);
                    }

                    //to sort if array found
                    else {
                        //logic path is path which we add in on condition to find the element value according to which we are sorting
                        var logicPath = pH.slice(cur.length + 1, pH.length),

                            logic = function(a) {
                                var val = jsonQ.pathValue(a, logicPath);

                                //to convert val to be compared
                                val = sortFunc.baseConv(type, val, settings);
                                return settings.logic(val);
                            };

                        //to sort the root json
                        sortFunc.sortAry(ary, logic, settings);

                        //if multilevel sort is true
                        if (settings.allLevel) {
                            //to maintain the path which is already sorted and change pathHolder to point first element of sorted array
                            pH[cur.length] = 0;
                            sortedPath.push(pathStr);
                        } else {
                            //remove sorted path
                            i = spliceElm(i);
                        }
                    }



                }
            }


            return jsonQ(jobj.jsonQ_root).find(key);
        }
function(key, settings) {
            //merge global setting with local setting
            settings = jsonQ.merge({}, jsonQ.settings.sort, settings);
            var jobj = this.find(key),
                current = jobj.clone(),
                sortStack = [],
                i, ln,
                sortedPath = [],
                type = objType(jobj.pathValue(current[0].path)),
                //function to get value which is an array from pathKey traversing from right.
                getClosestArray = function(key) {
                    while (key.length !== 0) {
                        var lastKey = key.pop();
                        if (!isNaN(lastKey)) {
                            var val = jobj.pathValue(key);
                            if (objType(val) == 'array') {
                                return val;
                            }
                        }
                    }
                    return null;
                };

            //initialize sort stack
            for (i = 0, ln = current.length; i < ln; i++) {
                sortStack.push({
                    pathHolder: current[i].path.concat([]),
                    current: current[i].path.concat([])
                });
            }

            //to run the loop until all are sorted
            var alpha = 0,

                // function to remove element if sorting is done for that path
                spliceElm = function(i) {
                    sortStack.splice(i, 1);
                    return --i;
                };
            while (sortStack.length !== 0) {
                alpha++;
                for (i = 0; i < sortStack.length; i++) {
                    var cur = sortStack[i].current,
                        pH = sortStack[i].pathHolder,
                        //to get the closest array in the current path. This will also change value of current path variable.
                        ary = getClosestArray(cur),
                        pathStr = cur.join();



                    //to remove from sort stack if no array is left on key or if that is already sorted
                    if (cur.length === 0 || sortedPath.indexOf(pathStr) != -1) {
                        i = spliceElm(i);
                    }

                    //to sort if array found
                    else {
                        //logic path is path which we add in on condition to find the element value according to which we are sorting
                        var logicPath = pH.slice(cur.length + 1, pH.length),

                            logic = function(a) {
                                var val = jsonQ.pathValue(a, logicPath);

                                //to convert val to be compared
                                val = sortFunc.baseConv(type, val, settings);
                                return settings.logic(val);
                            };

                        //to sort the root json
                        sortFunc.sortAry(ary, logic, settings);

                        //if multilevel sort is true
                        if (settings.allLevel) {
                            //to maintain the path which is already sorted and change pathHolder to point first element of sorted array
                            pH[cur.length] = 0;
                            sortedPath.push(pathStr);
                        } else {
                            //remove sorted path
                            i = spliceElm(i);
                        }
                    }



                }
            }


            return jsonQ(jobj.jsonQ_root).find(key);
        }
function ( key , settings ) { //merge global setting with local setting settings = jsonQ . merge ( { } , jsonQ . settings . sort , settings ) ; var jobj = this . find ( key ) , current = jobj . clone ( ) , sortStack = [ ] , i , ln , sortedPath = [ ] , type = objType ( jobj . pathValue ( current [ 0 ] . path ) ) , //function to get value which is an array from pathKey traversing from right. getClosestArray = function ( key ) { while ( key . length !== 0 ) { var lastKey = key . pop ( ) ; if ( ! isNaN ( lastKey ) ) { var val = jobj . pathValue ( key ) ; if ( objType ( val ) == 'array' ) { return val ; } } } return null ; } ; //initialize sort stack for ( i = 0 , ln = current . length ; i < ln ; i ++ ) { sortStack . push ( { pathHolder : current [ i ] . path . concat ( [ ] ) , current : current [ i ] . path . concat ( [ ] ) } ) ; } //to run the loop until all are sorted var alpha = 0 , // function to remove element if sorting is done for that path spliceElm = function ( i ) { sortStack . splice ( i , 1 ) ; return -- i ; } ; while ( sortStack . length !== 0 ) { alpha ++ ; for ( i = 0 ; i < sortStack . length ; i ++ ) { var cur = sortStack [ i ] . current , pH = sortStack [ i ] . pathHolder , //to get the closest array in the current path. This will also change value of current path variable. ary = getClosestArray ( cur ) , pathStr = cur . join ( ) ; //to remove from sort stack if no array is left on key or if that is already sorted if ( cur . length === 0 || sortedPath . indexOf ( pathStr ) != - 1 ) { i = spliceElm ( i ) ; } //to sort if array found else { //logic path is path which we add in on condition to find the element value according to which we are sorting var logicPath = pH . slice ( cur . length + 1 , pH . length ) , logic = function ( a ) { var val = jsonQ . pathValue ( a , logicPath ) ; //to convert val to be compared val = sortFunc . baseConv ( type , val , settings ) ; return settings . logic ( val ) ; } ; //to sort the root json sortFunc . sortAry ( ary , logic , settings ) ; //if multilevel sort is true if ( settings . allLevel ) { //to maintain the path which is already sorted and change pathHolder to point first element of sorted array pH [ cur . length ] = 0 ; sortedPath . push ( pathStr ) ; } else { //remove sorted path i = spliceElm ( i ) ; } } } } return jsonQ ( jobj . jsonQ_root ) . find ( key ) ; }
function to sort array objects
function to sort array objects