user823527
user823527

Reputation: 3712

Sort an array of objects by hour minutes in ascending order from earliest to latest

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

Upvotes: 1

Views: 3551

Answers (1)

Matt Ball
Matt Ball

Reputation: 359836

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

Upvotes: 8

Related Questions