Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array of days in ascending order

I have an array that contains unique strings of day names. The day names will be in random order. - eg:

["Sun 10am", "Sat 4pm", "Sat 10am", "Wed 3pm", "Sun 4pm"]

I want to use javascript to sort that array so that it will be in ascending order.

["Wed 3pm", "Sat 10am", "Sat 4pm", "Sun 10am", "Sun 4pm"]

Can anybody suggest the best way to do that?

Thank you

like image 441
Zhen Le Avatar asked May 27 '19 06:05

Zhen Le


People also ask

How do you sort an array in ascending order?

Example: Sort an Array in Java in Ascending Order Then, you should use the Arrays. sort() method to sort it. That's how you can sort an array in Java in ascending order using the Arrays. sort() method.

How do you sort dates in an array?

To sort an array of objects by date property: Call the sort() method on the array. Subtract the date in the second object from the date in the first. Return the result.

How do you list dates in ascending order?

Here's how to sort unsorted dates: Drag down the column to select the dates you want to sort. Click Home tab > arrow under Sort & Filter, and then click Sort Oldest to Newest, or Sort Newest to Oldest.

How to sort dates in an array in ascending order?

Given an array arr [] of N dates in the form of “DD-MM-YYYY”, the task is to sort these dates in ascending order. Recommended: Please try your approach on {IDE} first, before moving on to the solution. First compare the year of the two elements. The element with greater year will come after the other element.

How to sort an array in JavaScript?

As previously mentioned, the Array data structure in JavaScript has a built-in sort () method used to sort the elements of an array. The sort () method works by converting the elements into strings and sorting those string representations of elements lexicographically in ascending order.

How to sort elements in ascending order using Cout statement?

For this, we compare each element with the element lying next to it. If the value of the first element is greater than the second element, then their positions get interchanged. And the elements sorted in the ascending order are displayed on the screen using cout statement.

How do you sort an array in Excel VBA?

Sort Array A-Z (In Ascending Order) in Excel VBA First, we’ll convert the selected range from an Excel worksheet into an array. Dim MyArray As Variant MyArray = Application.Transpose (Selection) Next, we’ll sort the array by iterating through a for-loop.


2 Answers

  • You can create a object with days names as keys and values increasing from 1 to 7.

  • Create a helper function which takes second part of string i.e 3pm,10am.. as input and add 12 to result if its pm

  • Also divide the result by 24 so first sort should happen on days.
  • Use the sort() function and for each value add the time index(which will be always less than 1) to the day index(1,2,3...) and subtract it for both value.

const arr = ["Sun 10am", "Sat 4pm", "Sat 10am", "Wed 3pm", "Sun 4pm"];


function getTime(str){
  let time = str.slice(0,-2);
  let isAm = str.includes('am');
  return (isAm ? +time : +time + 12)/24
}

function sortTime(arr){
  let days = {
    Mon:1,
    Tue:2,
    Wed:3,
    Thur:4,
    Fri:5,
    Sat:6,
    Sun:7
  }
  return arr.slice().sort((a,b) => {
    let [day1,time1] = a.split(' ');
    let [day2,time2] = b.split(' ');
    return (days[day1] + getTime(time1)) - (days[day2] + getTime(time2)) 
  })
  
}

console.log(sortTime(arr))
like image 103
Maheer Ali Avatar answered Nov 15 '22 02:11

Maheer Ali


You could take an object for the values of the day and sort by the time.

const
    getTime = string => {
        var [day, hour, meridian] = string.match(/^(\D{3}) (\d+)(am|pm)/).slice(1),
            days = { Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6, Sun: 7 };

        return days[day] * 24 + +hour + (meridian === 'pm' && 12) + (hour === '12' && -12);
    };
var array = ["Sun 10am", "Sat 4pm", "Sat 10am", "Wed 3pm", "Sun 4pm", "Sat 12pm", "Sat 12am"];

array.sort((a, b) => getTime(a) - getTime(b));

console.log(array);
like image 20
Nina Scholz Avatar answered Nov 15 '22 04:11

Nina Scholz