R
R
Retr0Hacker2021-12-08 21:07:56
C++ / C#
Retr0Hacker, 2021-12-08 21:07:56

How to sort structure data by date if the date is given as a character string?

Urgent help needed:
Enter a sequence of records with information about some European countries. The date of the public holiday is specified as a character string in the form Day Month, for example August 24th. Print the entered list as a table. Then rearrange the data in calendar order according to the date of the main public holiday and print again

Question:
How to sort the structure data by date, if the date is specified as a character string?

Here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define N 25

int main()
{
  int count, n;

  struct country {
    char name[35];
    double population;
    char holl[25];
    char date[20];
  }list[N], per, * po1, * po2;

  system("chcp 1251");
  printf("Кол-во стран- ");
  scanf_s("%d", &n); 
  getchar();
  printf("\nДані: \n");
  for (count = 0; count < n; count++) {
    printf(" %d. Название страны: ", (count + 1));
    gets_s(list[count].name);
    printf(" %d. Население: ", (count + 1));
    scanf_s("%lf", &list[count].population);
    getchar();
    printf(" %d. Гос. праздник: ", (count + 1));
    gets_s(list[count].holl);
    printf(" %d. Дата: ", (count + 1));
    gets_s(list[count].date);
    printf("\n");
    rewind(stdin);
  }

  printf("\n\t\t\t\t\tТаблиця данных:\n\n");
  printf("  Название\t\tНаселение\t\tГос.праздник\t\t\tДата");
  for (count = 0; count < n; count++) {
    printf("\n %d. %s\t\t%.1lf\t\t%s\t\t%s", (count + 1), (list + count)->name, list[count].population, (list + count)->holl, (list + count)->date);
  }

  for (count = 0; count < n; count++) {
    po1 = list; 
    po2 = list + 3;
    for (; po2 < list + n - count; po1++, po2++)
      if (po1->date < po2->date) {
        per = *po1;
        *po1 = *po2;
        *po2 = per;
      }
  }
  printf("\n\n\t\t\Отсортированные данные(по дате):\n\n");
  printf("  Название\t\tНаселение\t\Гос.праздник\t\t\tДата");
  for (count = 0; count < n; count++) {
    printf("\n %d. %s\t\t%.1lf\t\t%s\t\t%s", (count + 1), (list + count)->name, list[count].population, (list + count)->holl, (list + count)->date);
  }
}
">

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2021-12-09
@jcmvbkbc

How to sort structure data by date if the date is given as a character string?

Strings like yyyymmdd can be sorted with qsort by comparing them with strcmp .

R
res2001, 2021-12-09
@res2001

1. Strings are compared using strcmp. When you compare like you have in your code, you are comparing pointer values, not what those pointers point to.
2. To compare numbers in string representation, the strings must be the same length, i.e. a number in a string representation with fewer characters must be left-padded with zeros to the length of the larger number (or pad both numbers to some fixed length).
3. When you have several numbers "glued" into a string (each of the numbers is padded with zeros), then the order of gluing for sorting should be from the largest entity to the smallest. In your example, the line should start with the month and then the day.
Only by applying all three points will you be able to correctly sort such lines.
All this is summarized jcmvbkbc in his answer.
But! Nothing prevents you from immediately converting the date into a numeric representation and sorting the numbers as you type. This will be much faster than solving text sorting problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question