Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8082777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:13:53+00:00 2026-06-05T17:13:53+00:00

I have this code to sort an array of objects. The data in the

  • 0

I have this code to sort an array of objects. The data in the objects is channel and time (hour, minutes). I want the sort to be by channels based on time from earliest to latest.

The channel data is accessed in this way:

channel_array[icount].data[0].hour
channel_array[icount].data[0].minutes

That data object array is like this and is already sorted:

[{hour:1, minutes:10},{hour:4, minutes:01}...]

Now all I need is to sort the channels from earliest to latest on the first element of the data array {hour:1, minutes: 10}. I do this with three nested loops. But this is not ideal. Is there a better way to do the sorting?

        var current_time = new Date();
        var current_hour = current_time.getHours();
        var comp_hour = current_hour - 1;
        for (var ih = 0; ih < 24; ih++) {
            comp_hour += 1;
            if (comp_hour == 24) { comp_hour = 0; }
            for (var minutes = 0; minutes < 60; minutes++) {
                for (var icount = 0; icount < channel_array.length; icount++) {
                    if (channel_array[icount].data.length > 0) {
                        var channel_hour = channel_array[icount].data[0].hour;
                        var channel_minutes = channel_array[icount].data[0].minutes;
                        var channel_phase = channel_array[icount].data[0].phase;
                        var next_day = channel_array[icount].data[0].next_day;
                        if (channel_phase.toLowerCase() == "pm" && channel_hour != 12) { channel_hour += 12; }
                        if ( parseInt(channel_hour) == parseInt(comp_hour) && parseInt(channel_minutes) == parseInt(minutes) && next_day != 1 ) {
                            channel_array_sort.push(channel_array[icount]); 
                        }
                    }           
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:13:55+00:00Added an answer on June 5, 2026 at 5:13 pm

    Good lord, this is overcomplicated! How about just passing a custom comparator to Array.sort?
    I’m honestly having a hard time figuring out exactly which array you are trying to sort, but in general, it would look something like this:

    var input = [{hour:1, minutes:10},{hour:4, minutes: 1}, ...];
    input.sort(function (a, b)
    {
        // compare hours first
        if (a.hour < b.hour) return -1;
        if (a.hour > b.hour) return 1;
    
        // else a.hour === b.hour, so compare minutes to break the tie
        if (a.minute < b.minute) return -1;
        if (a.minute > b.minute) return 1;
    
        // couldn't break the tie
        return 0;
    });
    

    N.B. this performs an in-place sort, which means that the original array is modified. If that’s not acceptable, just make a copy of the array before sorting it.

    var input = /* same as before */;
    var output = input.concat();
    output.sort(function ()
    {
        // same as before
    });
    

    Starting point for the solution, from the OP:

    channel_array_sort = channel_array.concat();
    
    channel_array_sort.sort(function (a, b)
    {
        if (a.data == undefined || b.data == undefined) return 0;
        if (a.data.length <= 0 || b.data.length <= 0) return 0;
    
        // compare hours first
        var a_hour = a.data[0].hour;
        if (a.data[0].phase == "pm") a_hour += 12;
        var b_hour = b.data[0].hour;
        if (b.data[0].phase == "pm") b_hour += 12;
    
        if (a_hour < b_hour) return -1;
        if (a_hour > b_hour) return 1;
    
        // else a.hour === b.hour, so compare minutes to break the tie
        if (a.data[0].minutes < b.data[0].minutes) return -1;
        if (a.data[0].minutes > b.data[0].minutes) return 1;
    
        // couldn't break the tie
        return 0;
    });
    
    var print_sort = JSON.stringify(channel_array_sort);                      
    alert('print_sort b '+print_sort);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello I have this code that use to sort a datatable. Dim sortingIndex As
I have this code: con.Open(); cmd = new MySqlCommand(String.Format(SELECT concat(name,'|',lastname) FROM {0} WHERE ID=
I have a line in a javascript function that sort an array of objects
In my C# code, I have a array of objects. And many of these
I'm trying to sort an array of people objects. The class looks like this.
I have an array of objects that I need to sort by a position
Possible Duplicate: How to sort an array of javascript objects? Every time I mess
This is the code: xml = REXML::Document.new(data) @contacts = Array.new xml.elements.each('//entry') do |entry| person
I have an array of objects which created from a custom class. The custom
I have this code: $(document).ready(function() { ofertas_sort = function(sort_key) { // array of offer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.