JSFiddle - React, Tailwind, and code Playground

by nirmalrizal

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

JavaScript

var timeArr = ["1:01 pm - 5:00 pm", "7:00 am - 8:00 am", "10:01 am - 1:00 pm", "8:01 am - 10:00 am"];

var sortedTime = [];
var tempTimesArr = [];

// Changing start time of the range into timestamp for comparison later and storing index of original time range to use original time range for later
timeArr.forEach(function(singleTime, index){
	var tempSingleTimeArr = singleTime.split("-");
  tempTimesArr.push({ time: moment(tempSingleTimeArr[0].trim(), "hh:mm a").unix(), index: index });
});

// sorts on the basis of start time of the range
tempTimesArr.sort(function(a, b){ return a.time - b.time; });

// Assign original time range to the new array
tempTimesArr.forEach(function(t){
	sortedTime.push(timeArr[t.index])
});
console.log(sortedTime);