Home > Software engineering >  Why my merge sort doesn't ordening descending?
Why my merge sort doesn't ordening descending?

Time:12-30

I searched a lot on the site but I didn't find any solution to my problem.

I have my code here:

function mergeSort(arr, prop, ord){
    if(arr.length <= 1){
        return arr;
    }

    let mid = Math.floor(arr.length / 2);

    let left = mergeSort(arr.slice(0, mid), prop, ord); 
    let right = mergeSort(arr.slice(mid), prop, ord); 

    return merge(left, right, prop, ord);
}

function merge(left, right, prop, ord){
    let newArr = [];

    while(left.length && right.length){
        if(left[0][prop] < right[0][prop]){
            newArr.push(left.shift());
        }else{
            newArr.push(right.shift());
        }
    }

    if(ord === 'asc'){
        return [...newArr, ...left, ...right];
    }else if(ord === 'desc'){
        return [...newArr, ...left, ...right];
    }
}

and I want to ordening this array:

let data = [
    {
        "color": "A",
        "door": 1,
        "wheel": 3,
        "year": 1963,
        "brand": "GMC",
        "sold": false,
        "owner": "Chalmers Boobyer",
        "motor": 10,
        "assembled": "20/08/2021"
    },
    {
        "color": "B",
        "door": 2,
        "wheel": 2,
        "year": 1980,
        "brand": "Ford",
        "sold": false,
        "owner": "Angelia Cromett",
        "motor": 12,
        "assembled": "02/05/2021"
    },
    {
        "color": "C",
        "door": 3,
        "wheel": 1,
        "year": 1999,
        "brand": "Audi",
        "sold": false,
        "owner": "Barth Pickring",
        "motor": 15,
        "assembled": "15/02/2021"
    },
    {
        "color": "D",
        "door": 4,
        "wheel": 1,
        "year": 2008,
        "brand": "Hyundai",
        "sold": true,
        "owner": "Aurore Soaper",
        "motor": 11,
        "assembled": "02/01/2019"
    }
];
console.log(mergeSort(data, 'motor', 'desc'));

This array is a part of my dataset.

I want ordening by descending, but if I want ordening by ascendent my code works well.

Why doesn't work in descending?

Thank you very much!

CodePudding user response:

You don't need any condition checking when merging those arrays in the merge function since the order you're providing is the same.

Then, since it's either ascending or descending, the eval() function simplifies that bit by transforming a predefined string containing either > or < to a statement which sorts it accordingly

function mergeSort(arr, prop, ord){
    if(arr.length <= 1){
        return arr;
    }

    let mid = Math.floor(arr.length / 2);

    let left = mergeSort(arr.slice(0, mid), prop, ord); 
    let right = mergeSort(arr.slice(mid), prop, ord); 

    return merge(left, right, prop, ord);
}

function merge(left, right, prop, ord){
    let newArr = [];

    while(left.length && right.length){
        let operator;
        operator = ord == "asc" ? "<" : ">"
        if(eval(`left[0][prop] ${operator} right[0][prop]`)){
            newArr.push(left.shift());
        } else {
            newArr.push(right.shift());
        }
    }
     
    return [...newArr, ...left, ...right];
}

let data = [
    {
        "color": "A",
        "door": 1,
        "wheel": 3,
        "year": 1963,
        "brand": "GMC",
        "sold": false,
        "owner": "Chalmers Boobyer",
        "motor": 10,
        "assembled": "20/08/2021"
    },
    {
        "color": "B",
        "door": 2,
        "wheel": 2,
        "year": 1980,
        "brand": "Ford",
        "sold": false,
        "owner": "Angelia Cromett",
        "motor": 12,
        "assembled": "02/05/2021"
    },
    {
        "color": "C",
        "door": 3,
        "wheel": 1,
        "year": 1999,
        "brand": "Audi",
        "sold": false,
        "owner": "Barth Pickring",
        "motor": 15,
        "assembled": "15/02/2021"
    },
    {
        "color": "D",
        "door": 4,
        "wheel": 1,
        "year": 2008,
        "brand": "Hyundai",
        "sold": true,
        "owner": "Aurore Soaper",
        "motor": 11,
        "assembled": "02/01/2019"
    }
];

console.log(mergeSort(data, 'motor', 'desc'));

  • Related