In an array of several elements of the structure

struct contact { char nomer[50]; char adress[50]; char sname[50]; }; 

The array is sorted by selecting the nomer field (the first 2 digits in the field can be repeated for several elements), and among the elements for which the first 2 digits of the nomer field nomer same, the sorting by the sname field sname alphabetically. I tried to count the number of elements with a match and start sorting them by the sname field. How can such a sort be implemented without error?

 #include "stdafx.h" #include<iostream> #include<cstring> #include<cstdlib> #include<string.h> using namespace std; struct contact { char nomer[50]; char adress[50]; char sname[50]; }; int base_output(contact *buf, int kol_el) { cout << "ΠŸΠΎΠ»Π½Ρ‹ΠΉ список ΠΊΠΎΠ½Ρ‚Π°ΠΊΡ‚ΠΎΠ² Π±Π°Π·Ρ‹ Π΄Π°Π½Π½Ρ‹Ρ…\n"; cout << "-----------------------------------------------------------\n"; for (int i = 0; i < kol_el; i++) { cout << (i + 1) << ".) " << buf[i].sname; cout << "\n" << buf[i].nomer; cout << "\n" << buf[i].adress; cout << "\n\n"; } cout << "-----------------------------------------------------------\n"; system("pause"); return 0; } void SelectSort(contact *buf, int kol_el) { int count = 0; char check_num[3] = "00"; for (int i = 0; i < kol_el - 1; i++) { int i_min = i; for (int j = i + 1; j < kol_el; j++) { if ((strcmp(buf[i_min].nomer, buf[j].nomer) > 0)) i_min = j; } if (i_min != i) { swap(buf[i], buf[i_min]); } if ((strncmp(buf[i_min].nomer, check_num, 2)) == 0) count++; else { for (int k = i - count; k < i - 1; k++) { int k_min = k; for (int l = (i - count + 1); l < i; k++) { if (strcmp(buf[l].sname, buf[k_min].sname) > 0) k_min = l; } if (k_min != k) swap(buf[k], buf[k_min]); } count = 1; strncpy(check_num, buf[i].nomer, 2); } } } int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL, "RUS"); FILE *f; f = fopen("telbook.dat", "r+b"); fseek(f, 0, SEEK_END); int size = ftell(f); int kol_el = size / sizeof(contact); contact *buf = new contact[kol_el]; fseek(f, 0, SEEK_SET); fread(buf, sizeof(contact), kol_el, f); fclose(f); int first = 0; int last = kol_el; unsigned int rezh = 0; do{ system("cls"); const int NotUsed = system("color 03"); cout << "1.ΠŸΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ Π±Π°Π·Ρƒ ΠΊΠΎΠ½Ρ‚Π°ΠΊΡ‚ΠΎΠ².\n"; cout << "2.Π‘ΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²ΠΊΠ° Π²Ρ‹Π±ΠΎΡ€ΠΎΠΌ\n"; cout << "3.Π’Ρ‹ΠΉΡ‚ΠΈ ΠΈΠ· ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹.\n"; cin >> rezh; switch (rezh) { case 1: { base_output(buf, kol_el); break; } case 2: { SelectSort(buf, kol_el); break; } } } while (rezh != 3); system("cls"); system("pause"); return 0; } 

    1 answer 1

    Just use one composite comparison function, and sort as usual.

    Sort of

     int contact_compare(const contact& c1, const contact& c2) { int cmp = strncmp(c1.nomer,c2.nomer,2); if (cmp != 0) return cmp; return strcmp(c1.sname,c2.sname); } 

    You can even write your own operators > , < , == for the type of contact :) And then - just the usual sorting with a single comparison using the specified function.