öncelikle bi yapı oluşturursanız bilgileri tutmak için daha iyi olur.
bu yapıları bi dizi içinde tutarsınız.
sonra sıralama için kullanacağınız algoritmayı seçin.
en sonda da isteğe göre sıralama yaptırırsınız. misal
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
struct ogrenci {
int no;
char ad[64];
char soyad[64];
ogrenci() {
}
ogrenci(int _no, const char* _ad, const char* _soyad) {
no = _no;
strcpy_s(ad, _ad);
strcpy_s(soyad, _soyad);
}
friend std::ostream& operator<<(std::ostream& os, const ogrenci& o) {
os << o.no << " - " << o.ad << ", " << o.soyad;
return os;
}
};
struct numaraya_gore_sirala {
bool operator()(const ogrenci& o1, const ogrenci& o2) {
return o1.no > o2.no;
}
};
struct ada_gore_sirala {
bool operator()(const ogrenci& o1, const ogrenci& o2) {
return strcmp(o1.ad, o2.ad) > 0;
}
};
//http://www.baskent.edu.tr/~tkaracay/etudio/ders/prg/dataStructures/sorting/BubbleSort/BubbleSort.pdf
template<class _func>
void bbSort(std::vector<ogrenci>& arr, _func fnc)
{
bool takas = true;
int j = 0;
ogrenci yedek;
while (takas) {
takas = false;
j++;
for (size_t i = 0; i < arr.size() - j; i++) {
if (fnc(arr[i], arr[i + 1])) {
yedek = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = yedek;
takas = true;
}
}
}
}
int main()
{
std::vector<ogrenci> ogrenciler;
ogrenciler.push_back(ogrenci(12,"Ahmet", "Mutlu"));
ogrenciler.push_back(ogrenci(9, "Didier", "Drogba"));
ogrenciler.push_back(ogrenci(10, "Burak", "Yilmaz"));
//***********************************************************
for (size_t i = 0; i < ogrenciler.size(); ++i) {
std::cout << ogrenciler[i] << std::endl;
}
//***********************************************************
bbSort(ogrenciler, ada_gore_sirala());
for (size_t i = 0; i < ogrenciler.size(); ++i) {
std::cout << ogrenciler[i] << std::endl;
}
//***********************************************************
bbSort(ogrenciler, numaraya_gore_sirala());
for (size_t i = 0; i < ogrenciler.size(); ++i) {
std::cout << ogrenciler[i] << std::endl;
}
return 0;
}