Помогите сделать сортировку по алфавиту!
Нужно сделать сортировку по алфавиту фамилий, которые мы вводим с клавиатуры, такое вообще возможно? чтобы сортировалось всё опираясь на введенные данные в "string NameSurname"
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
class NOTE {
public: string NameSurname, Number;
int Bday[3];
NOTE() {
NameSurname="Иванов Иван";
Number="+79872458766";
for (int i=0;
i < 2;
i++) {
Bday[i]=1;
}
Bday[2]=2001;
}
friend ostream& operator << (ostream&, const NOTE&);
friend istream& operator>>(istream&, const NOTE&);
};
ostream& operator << (ostream& os, NOTE& numb) //описание функции перегрузки оператора вывода
{
os << "ФИО: " << numb.NameSurname << endl << "Номер телефона: " << numb.Number << endl << "Дата рождения:";
for (int i=0;
i < 3;
i++) {
os << numb.Bday[i] << ".";
}
os << endl << endl;
return os;
}
istream& operator>>(istream& is, NOTE& student) //описание функции перегрузки оператора ввода
{
cin.ignore();
cout << "Введите ФИО: ";
getline(is, student.NameSurname);
cout << "Введите номер телефона: ";
is>>student.Number;
cout << "Введите дату рождения: ";
for (int i=0;
i < 3;
i++) {
is>>student.Bday[i];
}
return is;
}
int main() {
setlocale(LC_ALL, "RUS");
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
ifstream in("InZnak.txt");
ofstream out;
out.open("OutZnak.txt");
NOTE note[8]; //создаем массив класса
start: пункта меню system("cls");
cout << "Выберите пункт: " << endl;
cout << "1.Ввести данные вручную" << endl;
cout << "2.Отсортировать список по фамилиям" << endl;
cout << "3.Выход" << endl;
int vibor;
cout << "Выбор: ";
cin>>vibor;
system("cls");
switch (vibor) {
default: {
cout << "Неверная команда. Введите снова:" << endl;
system("pause");
goto start;
}
case 1: {
for (int i=0;
i < 8;
i++) {
cin>>note[i];
}
system("pause");
goto start;
}
case 2: //Сюда нужна сортировка по фамилиям
{
for (int i=0;
i < 8;
i++) {
for (int j=i + 1;
j < 8;
j++) {}
}
/*for (int i = 0; i < 8; i++)
{
for (int j = i + 1; j < 8; j++)
{
if (note[i].Bday[1] > note[j].Bday[1])
{ swap(note[i], note[j]); }
}
}*/
system("pause");
goto start;
}
case 3: {
cout << "Exit...";
return 0;
}
}
}
<!-- end snippet -->
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
NOTE note[8];
sort(note, note+8,
[](const auto& a, const auto& b){ return a.NameSurname < b.NameSurname; });
Примерно так...