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
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.
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.
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.
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.
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.
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.
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.
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
24
so first sort
should happen on days.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))
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With