-1

I was following a course and when i reached the challenge section the teacher asked to write down a code that sorts this objects array which contains some 'todos' with the properties: 'text, and completed (boolean)' and the sorting should work like this: the uncompleted todos (false) should be first and the the completed one should be the last. i wrote down a code and it actually worked and sorted the array as requested but when i saw how the instructor solved this challenge I was a bit confused because he used a different way so i wanted to ask the internet about which one (my code or the instructor's) is the best and more accurate. I'm actually confused because my code worked however I gave sort() function only one argument, or parameter (a) and it sorted the array as i wanted, so please i need an explanation

This is the basic objects-array:

// The basic objects array:

const todos = [{      
    text: 'wake up',
    completed: true
}, {
    text: 'get some food',
    completed: false
}, {
    text: 'play csgo',
    completed: false
}, {
    text: 'play minecraft',
    completed: true
}, {
    text: 'learn javascript',
    completed: false
}];

This is the code i wrote:

// My code

let sortTodos = function(todos) {
    todos.sort(function(a) {
        if (a.completed === false) {
            return -1;
        }
        else if (a.completed === true){
            return 1;
        }
        else {
            return 0;
        }
    })
}

sortTodos(todos);
console.log(todos); 

the instructor's code

// The instructor's code

const sortTodos = function (todos) {
    todos.sort(function (a, b) {
        if (!a.completed && b.completed) {
            return -1
        } else if (!b.completed && a.completed) {
            return 1
        } else {
            return 0
        }
    })
}

the output is :

[ { text: 'get some food', completed: false },
  { text: 'play csgo', completed: false },
  { text: 'learn javascript', completed: false },
  { text: 'play minecraft', completed: true },
  { text: 'wake up', completed: true } ]
Salah Eddine Makdour
  • 1,023
  • 1
  • 12
  • 24

1 Answers1

0

Although sort generally should use both arguments, your code happens to work because, as your logic shows, you only need to check the value of one of the items being compared, because you're only sorting the array into two segments, where the completed ones come first. If you know whether just one object being compared is completed or not, that's sufficient to know whether whether it should come before or after the other object being compared, no matter what the other object is.

That said, while using such an algorithm is possible, it's weird and confusing - better to explicitly sort by the difference in the completed property in both objects being compared:

const todos = [{      
    text: 'wake up',
    completed: true
}, {
    text: 'get some food',
    completed: false
}, {
    text: 'play csgo',
    completed: false
}, {
    text: 'play minecraft',
    completed: true
}, {
    text: 'learn javascript',
    completed: false
}];

todos.sort((a, b) => a.completed - b.completed);
console.log(todos);

If you want less computational complexity, you can do this in O(n) time rather than O(n log n) time pretty easily, just separate the array into two parts and put it back together:

const todos = [{      
    text: 'wake up',
    completed: true
}, {
    text: 'get some food',
    completed: false
}, {
    text: 'play csgo',
    completed: false
}, {
    text: 'play minecraft',
    completed: true
}, {
    text: 'learn javascript',
    completed: false
}];

const trues = [];
const falses = [];
todos.forEach((obj) => {
  (obj.completed ? trues : falses).push(obj);
});
console.log([...falses, ...trues]);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Thanks for the explanation now i can know why my code worked im so thankful – Salah Eddine Makdour Aug 24 '19 at 11:33
  • Note that if the sorted array needs to have any more than 2 chunks (one at the top, and one at the bottom) - for example, sort properties which may be `0, 1, 2` numerically, if you used `.sort` to sort them, you *would* need to use both parameters in `.sort`, because (eg) if an object being compared is `1`, you *would* need to know whether the other object is `0` or `2` to sort properly – CertainPerformance Aug 24 '19 at 11:39
  • thanks for clearing this for me now i can finish the course. – Salah Eddine Makdour Aug 24 '19 at 13:21