最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sort an array of objects by hour minutes in ascending order from earliest to latest - Stack Overflow

programmeradmin3浏览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 p_hour = current_hour - 1;
        for (var ih = 0; ih < 24; ih++) {
            p_hour += 1;
            if (p_hour == 24) { p_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(p_hour) && parseInt(channel_minutes) == parseInt(minutes) && next_day != 1 ) {
                            channel_array_sort.push(channel_array[icount]); 
                        }
                    }           
                }
            }
        }

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 p_hour = current_hour - 1;
        for (var ih = 0; ih < 24; ih++) {
            p_hour += 1;
            if (p_hour == 24) { p_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(p_hour) && parseInt(channel_minutes) == parseInt(minutes) && next_day != 1 ) {
                            channel_array_sort.push(channel_array[icount]); 
                        }
                    }           
                }
            }
        }
Share Improve this question edited Jun 15, 2012 at 1:23 user823527 asked Jun 15, 2012 at 0:27 user823527user823527 3,71217 gold badges69 silver badges111 bronze badges 2
  • Is the array you want to sort channel_array, or channel_array.data? If it's the former, could you provide more info on what unsorted input and sorted output should look like? – Matt Ball Commented Jun 15, 2012 at 0:35
  • I want to sort channel_array[icount] based on the sort results for the .data object. That object is already sorted. I want the channel sorted by hour minutes. – user823527 Commented Jun 15, 2012 at 1:16
Add a ment  | 

1 Answer 1

Reset to default 8

Good lord, this is overplicated! How about just passing a custom parator 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)
{
    // pare hours first
    if (a.hour < b.hour) return -1;
    if (a.hour > b.hour) return 1;

    // else a.hour === b.hour, so pare 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;

    // pare 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 pare 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)

  1. 暂无评论