Sort multiple range of time












0















I am currently working on a booking application project. I want to sort the itineraries created based on their time range, does anyone know how to sort multiple ranges of time in javascript/jquery?



For example, I have the ff. range of time (Each range should be considered as one object, I think?);



1:01 pm - 5:00 pm,
7:00 am - 8:00 am,
10:01 am - 1:00 pm,
8:01 am - 10:00 am,


enter image description here
I am planning to have a button to sort it from earliest to latest.



Something like this;



7:00 am - 8:00 am,
8:01 am - 10:00 am,
10:01 am - 1:00 pm,
1:01 pm - 5:00 pm


enter image description here



I haven't tried anything yet because I really don't know where to start. I am hoping to get the idea on how it works. Thank you in advance.





I did some research about this and I only found these How to sort an array of objects with jquery or javascript, What is the best way to parse a time into a Date object from user input in Javascript?. I am getting the idea on how to sort time because of this, but I don't know how to apply this on range of time.



As I have said, I really don't know where to start so I haven't considered whether it is an array or object, yet. It may be either one of them, I just need to know the idea on how it works. Thanks!










share|improve this question




















  • 2





    What sort of form is your input? An array, a multiline string, or what?

    – CertainPerformance
    Nov 16 '18 at 8:51






  • 1





    Put the times into a date object. Sort the date objects.

    – Mark Baijens
    Nov 16 '18 at 9:03













  • @certainperformance sorry, i edited my question

    – Royts
    Nov 16 '18 at 9:09











  • @mark-baijens Yes I can do that in a single time, but i need it in range of time.

    – Royts
    Nov 16 '18 at 9:09








  • 1





    @Royts then calucalte the range from the dateobject and sort that? Please post some code of your current situation at least.

    – Mark Baijens
    Nov 16 '18 at 9:15


















0















I am currently working on a booking application project. I want to sort the itineraries created based on their time range, does anyone know how to sort multiple ranges of time in javascript/jquery?



For example, I have the ff. range of time (Each range should be considered as one object, I think?);



1:01 pm - 5:00 pm,
7:00 am - 8:00 am,
10:01 am - 1:00 pm,
8:01 am - 10:00 am,


enter image description here
I am planning to have a button to sort it from earliest to latest.



Something like this;



7:00 am - 8:00 am,
8:01 am - 10:00 am,
10:01 am - 1:00 pm,
1:01 pm - 5:00 pm


enter image description here



I haven't tried anything yet because I really don't know where to start. I am hoping to get the idea on how it works. Thank you in advance.





I did some research about this and I only found these How to sort an array of objects with jquery or javascript, What is the best way to parse a time into a Date object from user input in Javascript?. I am getting the idea on how to sort time because of this, but I don't know how to apply this on range of time.



As I have said, I really don't know where to start so I haven't considered whether it is an array or object, yet. It may be either one of them, I just need to know the idea on how it works. Thanks!










share|improve this question




















  • 2





    What sort of form is your input? An array, a multiline string, or what?

    – CertainPerformance
    Nov 16 '18 at 8:51






  • 1





    Put the times into a date object. Sort the date objects.

    – Mark Baijens
    Nov 16 '18 at 9:03













  • @certainperformance sorry, i edited my question

    – Royts
    Nov 16 '18 at 9:09











  • @mark-baijens Yes I can do that in a single time, but i need it in range of time.

    – Royts
    Nov 16 '18 at 9:09








  • 1





    @Royts then calucalte the range from the dateobject and sort that? Please post some code of your current situation at least.

    – Mark Baijens
    Nov 16 '18 at 9:15
















0












0








0








I am currently working on a booking application project. I want to sort the itineraries created based on their time range, does anyone know how to sort multiple ranges of time in javascript/jquery?



For example, I have the ff. range of time (Each range should be considered as one object, I think?);



1:01 pm - 5:00 pm,
7:00 am - 8:00 am,
10:01 am - 1:00 pm,
8:01 am - 10:00 am,


enter image description here
I am planning to have a button to sort it from earliest to latest.



Something like this;



7:00 am - 8:00 am,
8:01 am - 10:00 am,
10:01 am - 1:00 pm,
1:01 pm - 5:00 pm


enter image description here



I haven't tried anything yet because I really don't know where to start. I am hoping to get the idea on how it works. Thank you in advance.





I did some research about this and I only found these How to sort an array of objects with jquery or javascript, What is the best way to parse a time into a Date object from user input in Javascript?. I am getting the idea on how to sort time because of this, but I don't know how to apply this on range of time.



As I have said, I really don't know where to start so I haven't considered whether it is an array or object, yet. It may be either one of them, I just need to know the idea on how it works. Thanks!










share|improve this question
















I am currently working on a booking application project. I want to sort the itineraries created based on their time range, does anyone know how to sort multiple ranges of time in javascript/jquery?



For example, I have the ff. range of time (Each range should be considered as one object, I think?);



1:01 pm - 5:00 pm,
7:00 am - 8:00 am,
10:01 am - 1:00 pm,
8:01 am - 10:00 am,


enter image description here
I am planning to have a button to sort it from earliest to latest.



Something like this;



7:00 am - 8:00 am,
8:01 am - 10:00 am,
10:01 am - 1:00 pm,
1:01 pm - 5:00 pm


enter image description here



I haven't tried anything yet because I really don't know where to start. I am hoping to get the idea on how it works. Thank you in advance.





I did some research about this and I only found these How to sort an array of objects with jquery or javascript, What is the best way to parse a time into a Date object from user input in Javascript?. I am getting the idea on how to sort time because of this, but I don't know how to apply this on range of time.



As I have said, I really don't know where to start so I haven't considered whether it is an array or object, yet. It may be either one of them, I just need to know the idea on how it works. Thanks!







javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 10:00







Royts

















asked Nov 16 '18 at 8:50









RoytsRoyts

173210




173210








  • 2





    What sort of form is your input? An array, a multiline string, or what?

    – CertainPerformance
    Nov 16 '18 at 8:51






  • 1





    Put the times into a date object. Sort the date objects.

    – Mark Baijens
    Nov 16 '18 at 9:03













  • @certainperformance sorry, i edited my question

    – Royts
    Nov 16 '18 at 9:09











  • @mark-baijens Yes I can do that in a single time, but i need it in range of time.

    – Royts
    Nov 16 '18 at 9:09








  • 1





    @Royts then calucalte the range from the dateobject and sort that? Please post some code of your current situation at least.

    – Mark Baijens
    Nov 16 '18 at 9:15
















  • 2





    What sort of form is your input? An array, a multiline string, or what?

    – CertainPerformance
    Nov 16 '18 at 8:51






  • 1





    Put the times into a date object. Sort the date objects.

    – Mark Baijens
    Nov 16 '18 at 9:03













  • @certainperformance sorry, i edited my question

    – Royts
    Nov 16 '18 at 9:09











  • @mark-baijens Yes I can do that in a single time, but i need it in range of time.

    – Royts
    Nov 16 '18 at 9:09








  • 1





    @Royts then calucalte the range from the dateobject and sort that? Please post some code of your current situation at least.

    – Mark Baijens
    Nov 16 '18 at 9:15










2




2





What sort of form is your input? An array, a multiline string, or what?

– CertainPerformance
Nov 16 '18 at 8:51





What sort of form is your input? An array, a multiline string, or what?

– CertainPerformance
Nov 16 '18 at 8:51




1




1





Put the times into a date object. Sort the date objects.

– Mark Baijens
Nov 16 '18 at 9:03







Put the times into a date object. Sort the date objects.

– Mark Baijens
Nov 16 '18 at 9:03















@certainperformance sorry, i edited my question

– Royts
Nov 16 '18 at 9:09





@certainperformance sorry, i edited my question

– Royts
Nov 16 '18 at 9:09













@mark-baijens Yes I can do that in a single time, but i need it in range of time.

– Royts
Nov 16 '18 at 9:09







@mark-baijens Yes I can do that in a single time, but i need it in range of time.

– Royts
Nov 16 '18 at 9:09






1




1





@Royts then calucalte the range from the dateobject and sort that? Please post some code of your current situation at least.

– Mark Baijens
Nov 16 '18 at 9:15







@Royts then calucalte the range from the dateobject and sort that? Please post some code of your current situation at least.

– Mark Baijens
Nov 16 '18 at 9:15














3 Answers
3






active

oldest

votes


















1














One solution using Moment.js looks like this.






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);

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





You can check the sorted list on the console of this fiddle https://jsfiddle.net/nirmalrizal/3z90gpre/27/






share|improve this answer
























  • yeah, i am using moment.js. I'll try this. Thanks!

    – Royts
    Nov 16 '18 at 9:49



















1














You can write your own sort function and sort the array based on a calculation between the start time and endtime to sort it based on the time range.






var ranges = [{
starttime: new Date(0,0,0,9,30,0),
endtime: new Date(0,0,0,17,30,0),
},{
starttime: new Date(0,0,0,7,30,0),
endtime: new Date(0,0,0,8,30,0),
},{
starttime: new Date(0,0,0,11,30,0),
endtime: new Date(0,0,0,15,30,0),
}];

function compare(a,b) {
if (a.endtime - a.starttime < b.endtime - b.starttime)
return -1;
if (a.endtime - a.starttime > b.endtime - b.starttime)
return 1;
return 0;
}

ranges.sort(compare);

console.log(ranges)








share|improve this answer
























  • Thanks for this. I'll try it now.

    – Royts
    Nov 16 '18 at 9:49



















0














I modified @MarkBaijens answer because it only sort the startTime. It doesn't sort correctly if there are same multiple starttime with different endtime.






var timeArr = ["1:01 pm - 5:00 pm", "7:00 am - 8:00 am", "10:01 am - 1:00 pm", "7:00 am - 8:30 am", "8:01 am - 10:00 am", "7:00 am - 7:50 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({ startTime: moment(tempSingleTimeArr[0].trim(), "hh:mm a").unix(), endTime: moment(tempSingleTimeArr[1].trim(), "hh:mm a").unix(), index: index });
});

// sorts on the basis of start time of the range
tempTimesArr.sort(function(a, b){ return ((a.startTime - b.startTime) === 0) ? (a.endTime - b.endTime) : (a.startTime - b.startTime);});

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

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





This is the link on codepen






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334328%2fsort-multiple-range-of-time%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    One solution using Moment.js looks like this.






    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);

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





    You can check the sorted list on the console of this fiddle https://jsfiddle.net/nirmalrizal/3z90gpre/27/






    share|improve this answer
























    • yeah, i am using moment.js. I'll try this. Thanks!

      – Royts
      Nov 16 '18 at 9:49
















    1














    One solution using Moment.js looks like this.






    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);

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





    You can check the sorted list on the console of this fiddle https://jsfiddle.net/nirmalrizal/3z90gpre/27/






    share|improve this answer
























    • yeah, i am using moment.js. I'll try this. Thanks!

      – Royts
      Nov 16 '18 at 9:49














    1












    1








    1







    One solution using Moment.js looks like this.






    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);

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





    You can check the sorted list on the console of this fiddle https://jsfiddle.net/nirmalrizal/3z90gpre/27/






    share|improve this answer













    One solution using Moment.js looks like this.






    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);

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





    You can check the sorted list on the console of this fiddle https://jsfiddle.net/nirmalrizal/3z90gpre/27/






    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);

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





    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);

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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 9:27









    Nirmal RijalNirmal Rijal

    587




    587













    • yeah, i am using moment.js. I'll try this. Thanks!

      – Royts
      Nov 16 '18 at 9:49



















    • yeah, i am using moment.js. I'll try this. Thanks!

      – Royts
      Nov 16 '18 at 9:49

















    yeah, i am using moment.js. I'll try this. Thanks!

    – Royts
    Nov 16 '18 at 9:49





    yeah, i am using moment.js. I'll try this. Thanks!

    – Royts
    Nov 16 '18 at 9:49













    1














    You can write your own sort function and sort the array based on a calculation between the start time and endtime to sort it based on the time range.






    var ranges = [{
    starttime: new Date(0,0,0,9,30,0),
    endtime: new Date(0,0,0,17,30,0),
    },{
    starttime: new Date(0,0,0,7,30,0),
    endtime: new Date(0,0,0,8,30,0),
    },{
    starttime: new Date(0,0,0,11,30,0),
    endtime: new Date(0,0,0,15,30,0),
    }];

    function compare(a,b) {
    if (a.endtime - a.starttime < b.endtime - b.starttime)
    return -1;
    if (a.endtime - a.starttime > b.endtime - b.starttime)
    return 1;
    return 0;
    }

    ranges.sort(compare);

    console.log(ranges)








    share|improve this answer
























    • Thanks for this. I'll try it now.

      – Royts
      Nov 16 '18 at 9:49
















    1














    You can write your own sort function and sort the array based on a calculation between the start time and endtime to sort it based on the time range.






    var ranges = [{
    starttime: new Date(0,0,0,9,30,0),
    endtime: new Date(0,0,0,17,30,0),
    },{
    starttime: new Date(0,0,0,7,30,0),
    endtime: new Date(0,0,0,8,30,0),
    },{
    starttime: new Date(0,0,0,11,30,0),
    endtime: new Date(0,0,0,15,30,0),
    }];

    function compare(a,b) {
    if (a.endtime - a.starttime < b.endtime - b.starttime)
    return -1;
    if (a.endtime - a.starttime > b.endtime - b.starttime)
    return 1;
    return 0;
    }

    ranges.sort(compare);

    console.log(ranges)








    share|improve this answer
























    • Thanks for this. I'll try it now.

      – Royts
      Nov 16 '18 at 9:49














    1












    1








    1







    You can write your own sort function and sort the array based on a calculation between the start time and endtime to sort it based on the time range.






    var ranges = [{
    starttime: new Date(0,0,0,9,30,0),
    endtime: new Date(0,0,0,17,30,0),
    },{
    starttime: new Date(0,0,0,7,30,0),
    endtime: new Date(0,0,0,8,30,0),
    },{
    starttime: new Date(0,0,0,11,30,0),
    endtime: new Date(0,0,0,15,30,0),
    }];

    function compare(a,b) {
    if (a.endtime - a.starttime < b.endtime - b.starttime)
    return -1;
    if (a.endtime - a.starttime > b.endtime - b.starttime)
    return 1;
    return 0;
    }

    ranges.sort(compare);

    console.log(ranges)








    share|improve this answer













    You can write your own sort function and sort the array based on a calculation between the start time and endtime to sort it based on the time range.






    var ranges = [{
    starttime: new Date(0,0,0,9,30,0),
    endtime: new Date(0,0,0,17,30,0),
    },{
    starttime: new Date(0,0,0,7,30,0),
    endtime: new Date(0,0,0,8,30,0),
    },{
    starttime: new Date(0,0,0,11,30,0),
    endtime: new Date(0,0,0,15,30,0),
    }];

    function compare(a,b) {
    if (a.endtime - a.starttime < b.endtime - b.starttime)
    return -1;
    if (a.endtime - a.starttime > b.endtime - b.starttime)
    return 1;
    return 0;
    }

    ranges.sort(compare);

    console.log(ranges)








    var ranges = [{
    starttime: new Date(0,0,0,9,30,0),
    endtime: new Date(0,0,0,17,30,0),
    },{
    starttime: new Date(0,0,0,7,30,0),
    endtime: new Date(0,0,0,8,30,0),
    },{
    starttime: new Date(0,0,0,11,30,0),
    endtime: new Date(0,0,0,15,30,0),
    }];

    function compare(a,b) {
    if (a.endtime - a.starttime < b.endtime - b.starttime)
    return -1;
    if (a.endtime - a.starttime > b.endtime - b.starttime)
    return 1;
    return 0;
    }

    ranges.sort(compare);

    console.log(ranges)





    var ranges = [{
    starttime: new Date(0,0,0,9,30,0),
    endtime: new Date(0,0,0,17,30,0),
    },{
    starttime: new Date(0,0,0,7,30,0),
    endtime: new Date(0,0,0,8,30,0),
    },{
    starttime: new Date(0,0,0,11,30,0),
    endtime: new Date(0,0,0,15,30,0),
    }];

    function compare(a,b) {
    if (a.endtime - a.starttime < b.endtime - b.starttime)
    return -1;
    if (a.endtime - a.starttime > b.endtime - b.starttime)
    return 1;
    return 0;
    }

    ranges.sort(compare);

    console.log(ranges)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 9:27









    Mark BaijensMark Baijens

    7,133103554




    7,133103554













    • Thanks for this. I'll try it now.

      – Royts
      Nov 16 '18 at 9:49



















    • Thanks for this. I'll try it now.

      – Royts
      Nov 16 '18 at 9:49

















    Thanks for this. I'll try it now.

    – Royts
    Nov 16 '18 at 9:49





    Thanks for this. I'll try it now.

    – Royts
    Nov 16 '18 at 9:49











    0














    I modified @MarkBaijens answer because it only sort the startTime. It doesn't sort correctly if there are same multiple starttime with different endtime.






    var timeArr = ["1:01 pm - 5:00 pm", "7:00 am - 8:00 am", "10:01 am - 1:00 pm", "7:00 am - 8:30 am", "8:01 am - 10:00 am", "7:00 am - 7:50 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({ startTime: moment(tempSingleTimeArr[0].trim(), "hh:mm a").unix(), endTime: moment(tempSingleTimeArr[1].trim(), "hh:mm a").unix(), index: index });
    });

    // sorts on the basis of start time of the range
    tempTimesArr.sort(function(a, b){ return ((a.startTime - b.startTime) === 0) ? (a.endTime - b.endTime) : (a.startTime - b.startTime);});

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

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





    This is the link on codepen






    share|improve this answer




























      0














      I modified @MarkBaijens answer because it only sort the startTime. It doesn't sort correctly if there are same multiple starttime with different endtime.






      var timeArr = ["1:01 pm - 5:00 pm", "7:00 am - 8:00 am", "10:01 am - 1:00 pm", "7:00 am - 8:30 am", "8:01 am - 10:00 am", "7:00 am - 7:50 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({ startTime: moment(tempSingleTimeArr[0].trim(), "hh:mm a").unix(), endTime: moment(tempSingleTimeArr[1].trim(), "hh:mm a").unix(), index: index });
      });

      // sorts on the basis of start time of the range
      tempTimesArr.sort(function(a, b){ return ((a.startTime - b.startTime) === 0) ? (a.endTime - b.endTime) : (a.startTime - b.startTime);});

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

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





      This is the link on codepen






      share|improve this answer


























        0












        0








        0







        I modified @MarkBaijens answer because it only sort the startTime. It doesn't sort correctly if there are same multiple starttime with different endtime.






        var timeArr = ["1:01 pm - 5:00 pm", "7:00 am - 8:00 am", "10:01 am - 1:00 pm", "7:00 am - 8:30 am", "8:01 am - 10:00 am", "7:00 am - 7:50 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({ startTime: moment(tempSingleTimeArr[0].trim(), "hh:mm a").unix(), endTime: moment(tempSingleTimeArr[1].trim(), "hh:mm a").unix(), index: index });
        });

        // sorts on the basis of start time of the range
        tempTimesArr.sort(function(a, b){ return ((a.startTime - b.startTime) === 0) ? (a.endTime - b.endTime) : (a.startTime - b.startTime);});

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

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





        This is the link on codepen






        share|improve this answer













        I modified @MarkBaijens answer because it only sort the startTime. It doesn't sort correctly if there are same multiple starttime with different endtime.






        var timeArr = ["1:01 pm - 5:00 pm", "7:00 am - 8:00 am", "10:01 am - 1:00 pm", "7:00 am - 8:30 am", "8:01 am - 10:00 am", "7:00 am - 7:50 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({ startTime: moment(tempSingleTimeArr[0].trim(), "hh:mm a").unix(), endTime: moment(tempSingleTimeArr[1].trim(), "hh:mm a").unix(), index: index });
        });

        // sorts on the basis of start time of the range
        tempTimesArr.sort(function(a, b){ return ((a.startTime - b.startTime) === 0) ? (a.endTime - b.endTime) : (a.startTime - b.startTime);});

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

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





        This is the link on codepen






        var timeArr = ["1:01 pm - 5:00 pm", "7:00 am - 8:00 am", "10:01 am - 1:00 pm", "7:00 am - 8:30 am", "8:01 am - 10:00 am", "7:00 am - 7:50 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({ startTime: moment(tempSingleTimeArr[0].trim(), "hh:mm a").unix(), endTime: moment(tempSingleTimeArr[1].trim(), "hh:mm a").unix(), index: index });
        });

        // sorts on the basis of start time of the range
        tempTimesArr.sort(function(a, b){ return ((a.startTime - b.startTime) === 0) ? (a.endTime - b.endTime) : (a.startTime - b.startTime);});

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

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





        var timeArr = ["1:01 pm - 5:00 pm", "7:00 am - 8:00 am", "10:01 am - 1:00 pm", "7:00 am - 8:30 am", "8:01 am - 10:00 am", "7:00 am - 7:50 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({ startTime: moment(tempSingleTimeArr[0].trim(), "hh:mm a").unix(), endTime: moment(tempSingleTimeArr[1].trim(), "hh:mm a").unix(), index: index });
        });

        // sorts on the basis of start time of the range
        tempTimesArr.sort(function(a, b){ return ((a.startTime - b.startTime) === 0) ? (a.endTime - b.endTime) : (a.startTime - b.startTime);});

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

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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 7:47









        RoytsRoyts

        173210




        173210






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334328%2fsort-multiple-range-of-time%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine