/**
 * @param {number[][]} intervals
 * @return {number[][]}
 */
var cmp = function(a,b){
    if(a[0]==b[0]){
        return a[1]-b[1];
    }else{
        return a[0]-b[0];
    }
};

var merge = function(intervals) {
    let newInter = intervals.sort(cmp);
    // console.log('wscnbbbbb',newInter);
    let resList = [];
    let curNode = newInter.shift();
    resList.unshift(curNode);
    while(newInter.length){
        let lastNode = resList[0];
        curNode = newInter.shift();
        if(curNode[0]<=lastNode[1]){
            let newStart = lastNode[0];
            let newEnd = Math.max(lastNode[1],curNode[1]);
            resList.shift();
            resList.unshift([newStart,newEnd]);
        } else {
            resList.unshift(curNode);
        }
    }
    return resList;
};