Sorting an Array by Input Value in JavaScript

Zach Weber
4 min readAug 4, 2020

--

This is a continuation of my new series of posts on algorithms. You can check out my previous one about finding the first non-repeating character in a string here.

While preparing for a technical interview this past week, I wanted to focus on sorting arrays. I came across this prompt and hated it at the start, but I came to appreciate how much it made me think. On top of that, I’m pretty happy with how I solved it!

The Prompt

You have some tasks in your account. For each ith of them you know its deadlinesi, which is the last day by which it should be completed. As you can see in your calendar, today's date is day. The program labels each task in accordance with its due date:

  • If the task is due today or it’s already overdue, it is labeled as Today;
  • If the task is due within a week but not today — that is, its deadline is somewhere between day + 1 and day + 7 both inclusive - it is labeled as Upcoming;
  • All other tasks are labeled as Later;

Given an array of deadlines and today's date day, your goal is to find the number of tasks with each label type and return it as an array with the format [Today, Upcoming, Later], where Today, Upcoming and Later are the number of tasks that correspond to that label.

Example

  • For deadlines = [1, 2, 3, 4, 5] and day = 2, the output should be
    tasksTypes(deadlines, day) = [2, 3, 0].
  • Today is day 2, so tasks with deadlines at 1 and 2 are labeled as Today. The other three tasks should be completed within a week, so they should be labeled as Upcoming. There are no tasks labeled as Later.
Respectfully borrowed from CodeSignal
  • For deadlines = [1, 2, 4, 2, 10, 3, 1, 4, 5, 4, 9, 8] and day = 1, the output should be
    tasksTypes(deadlines, day) = [2, 8, 2].
  • Today is day 1, which means that the two tasks with a deadline of 1 are labeled as Today. Tasks with deadlines 10 and 9 are labeled as Later, since their deadlines are more than 7 days ahead. The other eight tasks are labeled as Upcoming.
Respectfully borrowed from CodeSignal

My Solution

What a prompt, right? Luckily it’s much less complicated than the prompt makes it seem (in my opinion).

function tasksTypes(deadlines, day) {
let today = 0;
let upcoming = 0;
let later = 0;
const deadline = [];
for (let i = 0; i < deadlines.length; i++) {
let dline = deadlines[i];
if (dline <= day) {
today += 1
} else if (dline >= day+1 && dline <= day+7) {
upcoming += 1
} else {
later += 1
}
}
deadline.push(today);
deadline.push(upcoming);
deadline.push(later);
return deadline;
}
  • We know that the tasks are going to be sorted into 3 categories: today, upcoming, and later. As a result, the first thing I did was set variables for each of these initialized to 0, because they begin with 0 tasks assigned to them.
  • I also set the variable deadline to an empty array. This is where we’re going to push the values of today, upcoming, and later after we’ve sorted the input array.
  • To iterate through the data, I chose to use a for loop because I know how many iterations we need to make (the length of the input array deadlines).
  • To isolate the individual deadline, I set the variable dline equal to the current index place.
  • Next, I used a series of if statements to sort the deadlines array. The first statement dictates that today should be added to if the current index value in the deadlines array is less than or equal to the value of day. The next statement dictates that upcoming should be added to if the current index value in the deadlines array is greater than or equal the value of day + 1 and less than or equal to the value of day + 7. The last statement covers the remaining parameters and adds 1 to later.
  • Following the if statement, I pushed the values of today, upcoming, and later (in that order due to the constraints of the prompt and, therefore, tests) into the deadline array.
  • Finally, I returned the deadline array that’s been populated with the values associated with today, upcoming, and later.

This solution passed all 20 tests within the allotted 4 second run time. If you have another solution, please let me know! Happy coding!

--

--

Zach Weber

Denver-based Software Engineer, nature lover, and lifelong learner