const  sortData = {
    result : [],
    type : {},
    parse(array , column , i) {
        let _this=this;
        let data = {};
        data['0']='';data['1']='';data['2']='';data['3']='';data['4']='';data['5']='';data['6']='';data['7']='';data['8']='';
        data['9']='';data['a']='';data['b']='';data['c']='';data['d']='';data['e']='';data['f']='';data['g']='';data['h']='';
        data['i']='';data['j']='';data['k']='';data['l']='';data['m']='';data['n']='';data['o']='';data['p']='';data['q']='';
        data['r']='';data['s']='';data['t']='';data['u']='';data['v']='';data['w']='';data['x']='';data['y']='';data['z']='';
        data['A']='';data['B']='';data['C']='';data['D']='';data['E']='';data['F']='';data['G']='';data['H']='';data['I']='';
        data['J']='';data['K']='';data['L']='';data['M']='';data['N']='';data['O']='';data['P']='';data['Q']='';data['R']='';
        data['S']='';data['T']='';data['U']='';data['V']='';data['W']='';data['X']='';data['Y']='';data['Z']='';
        array.forEach(function (obj) {
            if (!obj[column]) obj[column]='';
            if (!data[obj[column].charAt(i)])  data[obj[column].charAt(i)] = [obj];
            else data[obj[column].charAt(i)].push(obj);
        });
        let recursion=[];
        for (let key in data){
            if (!data[key]){
                delete data[key];
                continue;
            }
            if (data[key].length > 1){
                if ( !(!data[key].some(function (value, index) {
                    return value[column] !== data[key][0][column];
                }))){
                    recursion.push(key);
                }
            }
        }
        if (recursion.length > 0){
            i++;
            recursion.forEach(function (key) {
                data[key] = _this.parse(data[key] ,column , i);
            });
        }
        return data;
    },

    sort:function(array, column ,type, i=0 ) {
        this.result = [];
        this.type = type;
        //if (!Array.isArray(column)) column = [column];
        let columnName = column[0];
        column.splice(0,1);
        this.merge(this.parse(array, columnName, i), column, columnName);
        array.length = 0;
        array.push.apply(array,this.result);
    },

    merge(object, subordinate = [] ,columnName = '') {
        for (let key in object){
            if (Array.isArray(object[key])){
                if (object[key].length > 1 && subordinate.length > 0){
                    let subordinateName = subordinate[0];
                    subordinate.splice(0,1);
                    object[key] = this.merge(this.parse(object[key], subordinateName, 0), subordinate, subordinateName);
                }
                if (this.type[columnName] === 'asc') this.result.push.apply(this.result,object[key]);
                else this.result.unshift.apply(this.result,object[key]);
            }else this.merge(object[key], [] ,columnName);
        }
    }
};
module.exports=sortData;
