Jump to content
  • Advertisement
Sign in to follow this  
FGFS

sort list

This topic is 888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

I want to sort by color. Simply lower before higher numbers, only int 1-3

 

        Ausfalllinie[0].push_back(Ausfaelle(20,1,2,APdiscon,20, "autopilot_disconnect"));
        Ausfalllinie[1].push_back(Ausfaelle(21,1,1,APtrimDo,1*20, "autopilot_trim_down"));
        Ausfalllinie[2].push_back(Ausfaelle(22,1,3,Fuel,2*20, "fuel_pump_on"));

...

...

 

The class.h is:

class Ausfaelle {
public:
    int index, active, color, height;
    float drValue;
    std::string label;
    Ausfaelle(int index, int active, int color, float drValue, int height, std::string label);  

};
 

and the readout:

        for( int x = 0; x < 10; x = x + 1 )
        {
          for (std::list<Ausfaelle>::iterator it=Ausfalllinie[x].begin(); it != Ausfalllinie[x].end(); ++it)
    {
        Ausfaelle test = *it;

...

...

 

Many thanks

Edited by FGFS

Share this post


Link to post
Share on other sites
Advertisement

std::sort() does this for you. You can pass it a custom sort function.

 

The syntax is:

 std::sort(begin(myList), end(myList), YourSortFunction);

Using a lambda it can look like this:

struct MyStruct
{
   float value;
};

std::list<MyStruct> myList;

std::sort(begin(myList), end(myList),
[](const MyStruct &structA, const MyStruct &structB)
{
    return (structA.value < structB.value); //Replace with your sort criteria.
});

Note: Instead of using container.begin() and container.end(), the newest guidelines say to use std::begin(container) and std::end(container). It's more flexible, and works with things like normal arrays, and so on.

 

In the majority of situations where you'll be using it, you won't have to type std::, because the compiler can resolve that namespace for you on the begin()/end() functions.

Share this post


Link to post
Share on other sites
Have you tried std::sort?
 
std::sort(
   std::begin(Ausfalllinie[x]),
   std::end(Ausfalllinie[x]),
   [](const Ausfaelle& a, const Ausfaelle& b)
   {
       return a.color < b.color;
   });
edit: Ninja'd. Edited by Oberon_Command

Share this post


Link to post
Share on other sites

No, std::sort requires random access iterators. std::list only provides bidirectional iterators, which means you can't use them with std::sort. Use std::list::sort instead.

 

*facepalm* - Mea culpa.

I almost always use std::vector - I forgot about the iterator requirements.

Share this post


Link to post
Share on other sites

No, std::sort requires random access iterators. std::list only provides bidirectional iterators, which means you can't use them with std::sort. Use std::list::sort instead.

 
*facepalm* - Mea culpa.
I almost always use std::vector - I forgot about the iterator requirements.


Likewise. std::list is not something I see often, and for pretty good reason. I would be curious to know why OP is using std::lists here. Edited by Oberon_Command

Share this post


Link to post
Share on other sites

Hi

 I have the image scrolling but only want each image once. With the first on top

        for( int x = 0; x < 10; x = x + 1 )
        {
          for (std::list<Ausfaelle>::iterator it=Ausfalllinie[x].begin(); it != Ausfalllinie[x].end(); ++it)
    {
        Ausfaelle test = *it;

 

        if (test.drValue >= 0.2){

...

show image with x as height.

...

Cant' wrap my head around those two for. Also still couldn't get the list sorted by color.

Many thanks

 

Share this post


Link to post
Share on other sites

Thanks now the sorting is still not working. Also the first should be on top but I guess this will work after sort as well?

 

        for( int x = 0; x < 10; x = x + 1 )
        {
          for (std::list<Ausfaelle>::iterator it=Ausfalllinie[x].begin(); it != Ausfalllinie[x].end(); ++it)
    {

            Ausfaelle test = *it;

            Ausfalllinie[x].sort(compare(Ausfaelle &test, Ausfaelle &test2));

...

 

I get this error:

Multiple markers at this line
    - ‘test2’ was not declared in this scope

    - expected primary-expression before ‘&’

 

defined alike:

 

bool compare (Ausfaelle &ausfaelle1, Ausfaelle &ausfaelle2)
{
  return ( ausfaelle1.color < ausfaelle2.color );
}
 

Many thanks

Share this post


Link to post
Share on other sites

I get this error:
Multiple markers at this line
    - ‘test2’ was not declared in this scope
    - expected primary-expression before ‘&’

'test2' was not declared in the scope. The error message is not ambiguous. You can not just take the address of an undeclared variable like that.

What you want to do is pass the function itself, not call the function.

Ausfalllinie.sort(compare);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!