Help please sort by date of birth. I managed to sort only by year, but I need by date of birth.

#include <math.h> #include <string.h> #include <tchar.h> #include <conio.h> #include <locale> #include <stdio.h> #include <iostream> using namespace std; struct student_list { char first_name[15]; char second_name[15]; char two_name[15]; struct { unsigned int day,month,year; } brithday; }; int main() { FILE *input, *output; int i,n=5; char s[255]; struct student_list bd [10]; student_list sort[10]; input=fopen("test.txt","r"); i=0; while(i<n) { fscanf(input,"%s",&bd[i].first_name); fscanf(input,"%s",&bd[i].second_name); fscanf(input,"%s",&bd[i].two_name); fscanf(input,"%d",&bd[i].brithday.day); fscanf(input,"%d",&bd[i].brithday.month); fscanf(input,"%d",&bd[i].brithday.year); i++; } setlocale(LC_ALL,"Russian"); cout<<"\tСписок группы:"<<endl; cout<<"--------------------------------------------------------------------------------"<<endl; setlocale(LC_ALL,"Russian"); for(i=0;i<n;i++) { cout<<"№ "<<i+1<<"| Ф.И.О. студента: ";setlocale(LC_ALL,"Russian"); cout<<bd[i].second_name<<" "<<bd[i].first_name<<" "<<bd[i].two_name;setlocale(LC_ALL,"Russian"); cout<<"|Дата рождения: "<<bd[i].brithday.day<<"/"<<bd[i].brithday.month<<"/"<<bd[i].brithday.year<<"|"<<endl; } cout<<"------------------------------------------------------------------------------"; for (i=0;i<n-1;i++) { for (int g=i+1;g<n;g++) { if(bd[i].brithday.year>bd[g].brithday.year) { sort[1]=bd[i]; bd[i]=bd[g]; bd[g]=sort[1]; } } } cout<<"\tСписок группы упорядоченный по возрастанию году рождения :"<<endl; cout<<"--------------------------------------------------------------------------------"<<endl; setlocale(LC_ALL,"Russian"); for(i=0;i<n;i++) { cout<<"№ "<<i+1<<"| Ф.И.О. студента: "; setlocale(LC_ALL,"Russian"); cout<<bd[i].second_name<<bd[i].first_name<<bd[i].two_name; setlocale(LC_ALL,"Russian"); cout<<"|Дата рождения: "<<bd[i].brithday.day<<"/"<<bd[i].brithday.month<<"/"<<bd[i].brithday.year<<"|"<<endl; } cout<<"------------------------------------------------------------------------------"; _getch(); } 

    1 answer 1

    Well, you only need to correct the condition when sorting. Instead of bd[i].brithday.year>bd[g].brithday.year write something like

     bd[i].brithday.year>bd[g].brithday.year || bd[i].brithday.year == bd[g].brithday.year && bd[i].brithday.month>bd[g].brithday.month || bd[i].brithday.year == bd[g].brithday.year && bd[i].brithday.month == bd[g].brithday.month && bd[i].brithday.day>bd[g].brithday.day 

    You can use the date translation functions and make a comparison in one line, but in this case it is not necessary. Usually months are rarely more than 12 (exactly less than 20), and more than 31 days (exactly less than 50) a year. Therefore, you can do so

      bd[i].brithday.year * (20*50) + bd[i].brithday.month * 20 + bd[i].brithday.day > bd[g].brithday.year * (20*50) + bd[g].brithday.month * 20 + bd[g].brithday.day 

    But for serious code, it’s still better to use date decoding functions.

    • Thanks for the help, but for the first solution, the compiler swears (operand of assignment) - battlemanls
    • Found, Corrected - KoVadim
    • Thank you very much! Works! - battlemanls