0
#include <QtCore/QCoreApplication>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<cctype>
#include<iomanip>
using namespace std; 
void showArr();
void Sort_ind();
void sort_rule();

 struct Weather
    {
        char Date[12];
        int Temperature;
        int Pressure;
        int Humidity;
    };

    Weather arr[30] =
    {
        {"2002.12.15", -4, 115, 5},
        {"2014.5.3", 10, 101, 10},
        {"2001.8.3", 15, 68, 12},
        {"2013.11.9", 12, 123, 3},
    };
    int size = 4;

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        Sort_ind();
        return a.exec();
    }
    void showArr()
    {
        for(int i =0;i < size;i++)
        {
            cout <<setw(4)<<arr[i].Date<<" - Date"
                 <<setw(4)<<arr[i].Temperature<< " - Temperature"
                 <<setw(4)<<arr[i].Pressure<< "  - Pressure"
                 <<setw(4)<<arr[i].Humidity<< " -  % Humidity"<<endl;
        }
    }

    void Sort_ind()
    {
        int k;
        cout <<"0 - Exit\n";
        cout <<"1 - Sort temperature\n";
        cout <<"2 - Sort pressure\n";
        cout <<"3 - Sort humidity\n";
        cin >>k;
        switch(k)
        {
        case 0:exit(0);
        case 1:
                sort_rule() // here must be parametr.For example, we call
                showArr();  // sort function with parametr for Temperature 
                break;      // and sorts by  the right rule
    // and other case for indicators       
    }

    void sort_rule() // and here
    {
        for(int i =0;i< size;i++)
        {
            for(int j = i + 1;j<size;j++)
            {
                if() // compared indicators 
                {
                    swap(arr[i].Pressure, arr[j].Pressure);
                    swap(arr[i].Temperature, arr[j].Temperature);
                    swap(arr[i].Humidity, arr[j].Humidity);
                    swap(arr[i].Date, arr[j].Date);

                }

            }
        }
    }

I'm writting a simple programm with structures which can sort by 3 indicators, I'm thinking about writting a fuction which can do it to avoid repeating parts of my code, but there is a problem.I don't know how exactly I can pass fields from structure as a parametrs to sorting function. Show me how to do it if it possible.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Alex Delarge
  • 115
  • 1
  • 7
  • make `sort_rule` take an `enum` that indicates by which data member you are sorting? – Fureeish Jan 18 '18 at 14:39
  • This is a basic part of learning C++. – DingusKhan Jan 18 '18 at 14:40
  • Don't swap separate members of the structure. Swap the full structures instead. Like e.g. `swap(arr[i], arr[j])` – Some programmer dude Jan 18 '18 at 14:42
  • 2
    Not that you actually *need* to swap manually. Instead use [`std::sort`](http://en.cppreference.com/w/cpp/algorithm/sort) with a suitable [lambda expression](http://en.cppreference.com/w/cpp/language/lambda) to compare the structures. – Some programmer dude Jan 18 '18 at 14:44
  • It is also confusing as to why you have an array of 30 `Weather`s, but only populate 4, and have a separate `size`. I would recommend using `std::vector` instead. – Caleth Jan 18 '18 at 14:49
  • OT maybe you should not start a QCoreApplication eventloop. –  Jan 18 '18 at 15:32

0 Answers0