字符串排序,对时间字符串排序

3.对时间字符串排序:
排序前:"19:30 15:23 3:46 8:55 20:21 19:01"
排序后:"3:46 8:55 15:23 19:01 19:30 20:21"

供参考


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int mycmp(char *t, char *s)
{
    char str[10], *tmp = NULL;
    int at[2] = {0}, as[2] = {0};

    strcpy(str, t);
    tmp = strtok(str, ":");
    at[0] = atoi(tmp);

    tmp = strtok(NULL, ":");
    at[1] = atoi(tmp);

    strcpy(str, s);
    tmp = strtok(str, ":");
    as[0] = atoi(tmp);

    tmp = strtok(NULL, ":");
    as[10] = atoi(tmp);

    if (at[0] != as[0])
        return at[0] - as[0];
    else
        return at[1] - as[1];
}
int main()
{
    char str[] = "19:30 15:23 3:46 8:55 20:21 19:01";
    char strar[6][10] = {0};
    char *tmp = NULL;
    int count = 0;

    printf("%s\n", str);
    tmp = strtok(str, " ");
    strcpy(strar[0], tmp);
    count++;
    while (1)
    {
        tmp = strtok(NULL, " ");
        if (tmp == NULL)
            break;
        strcpy(strar[count], tmp);
        count++;
    }

    char t[10];
    for (int i = 0; i < count - 1; i++)
    {
        for (int j = i + 1; j < count; j++)
        {
            if (mycmp(strar[i], strar[j]) > 0)
            {
                strcpy(t, strar[i]);
                strcpy(strar[i], strar[j]);
                strcpy(strar[j], t);
            }
        }
    }
    for (int i = 0; i < count; i++)
        printf("%s ", strar[i]);

    printf("\n");

    return 0;
}