I want to sort a deque according to the int g value contained in the node struct. The structure of my program is this:
struct node
{
    int x;
    int y;
    int g;  
};
deque<node> open;
This is the sorting function I am trying but it gives garbage values. Please guide me:
deque<node> sort(deque<node> t)
{
    deque<node>::iterator it;
    int size= t.size();
    node te;
    for(int i=0; i<size; i++)
    {
        for(int j=0; j<size-i; j++)
        {
            if(t[j].g < t[j+1].g)
            {
                te.x = t[j].x;
                te.y = t[j].y;
                te.g = t[j].g;
                t[j].x = t[j+1].x;
                t[j].y = t[j+1].y;
                t[j].g = t[j+1].g;
                t[j+1].x = te.x;
                t[j+1].y = te.y;
                t[j+1].g = te.g;
            }
        }
    }
    for(it=t.begin();it!=t.end();it++)
    {   
        te = *it;
        cout<<te.x<<","<<te.y<<","<<te.g<<endl;
    }
    return t;
}
				
                        
In your code, you go out of bounds when
jiterates uptosize - 1, which results inj+1being equal tosizewhich is out of bounds.You can use
std::sortwhich is an inbuilt C++ function.The syntax is
std::sort(t.begin(), t.end())wheretis the object you want to sort.Since you are using a struct, you will have to define an ordering, i.e. how is less than and equal to calculated. You can either overload the inbuilt operators, or define a new function and pass that as a third parameter to the
sortfunction.