0

i have looked at a few examples with zero luck but i would like to perform a very simple task i would just like to sort a struct using the algorithm header

    struct MyStruct
    {
        int level; //sort according to this
        int m; 
        char ks;
    };
    int main() {

    MyStruct* ary = new MyStruct[SIZE];
    sort(ary, ary + tota_stored); //this part

        for (int j = 0; j <tota_stored; j++)
         {
           cout << ary[j].level<< ary[j].m<< ary[j].ks<< " \n"; //print sorted
         }

     return 0;
    }

i would like to sort it based on the level variable in my struct

kunz
  • 1,063
  • 1
  • 11
  • 27
  • std::sort takes a compare predicate so you can pass a custom operator. Knowing how to use such a predicate would solve your problem. What have you tried? – Alex Huszagh Aug 11 '17 at 20:51

2 Answers2

3

You can give a predicat to std::sort like

std::vector<MyStruct> ary(SIZE);
std::sort(ary.begin(), ary.end(), [](auto a, auto b){return a.level < b.level;});

First, I encourage you to do not use new, new[], delete, delete[] in C++ modern. It is better to use Container and Smart Pointers (I use std::vector here)

Second, what does mean : [](auto a, auto b){return a.level < b.level;}.

It is an "object" that is named a lambda. Lambda are a bit like function or something like that. This "function" takes two arguments : a and b, which them type are automatically deduced at compile time (thanks to auto).

The whole function is what we call a predicat. Here, you want to compare only the level part, that's why I am using a.level < b.level.

Also, you can check the documentation of std::sort function here : http://en.cppreference.com/w/cpp/algorithm/sort

As you can see, I am using this one :

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

Compare comp here is the predicat that I defined by a lambda.

More information about lambda here https://stackoverflow.com/a/7627218/4950448

However if like Michaël Roy suggests, you must use C++98, you can use a "functor" (not sure about the name)

struct CompareStructOperatorLess {
    bool operator()(const MyStruct &a, const MyStruct &b) const {
        return a.level < b.level;
    }
};
...
std::sort(ary.begin(), ary.end(), CompareStructOperatorLess());
Antoine Morrier
  • 3,930
  • 16
  • 37
2

In c++11 and later, you can use Antoine's lambda function based solution. If you have to use c++98, or if the level data member is the meaningful value for comparing your struct data, or if you expect to do a lot of comparisons, or if the comparison is more complex, you can create an operator to help order your struct values.

struct MyStruct
{
    int level; //sort according to this
    int m; 
    char ks;

    friend inline bool operator < (const Mystruct& a, const MysStruct& b)
    {
        return a.level < b.level;
    }
};

int main() 
{
    MyStruct* ary = new MyStruct[SIZE];
    size)t total_stored = SIZE;
    sort(ary, ary + total_stored);  // <-- operator < will be called.

    for (int j = 0; j <tota_stored; j++)
    {
        cout << ary[j].level<< ary[j].m<< ary[j].ks<< " \n"; //print sorted
    }

    return 0;
}
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19