0

This is my function that needs to print all elements from transports array of pointers which have bigger price than the Transport *t element

void pecatiPoloshiPonudi(Transport **ponudi, int brEl, Transport &T)
{
    //cout<<T.getDestinacija();
    for (int i=0;i<brEl;i++)
    {
        if(T.getPrice() < ponudi[i]->getPrice())
        {
            cout<<ponudi[i]->getDestinacija()<<" "<<ponudi[i]->getRastojanie()<<" "<<ponudi[i]->getPrice()<<endl;
        }
    }
}

I got this one, but i need it to sort it in ascending order, depending on their price.

  • Does this answer your question? [Sorting a vector of custom objects](https://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects) – scohe001 Sep 16 '20 at 14:30
  • @scohe001 Nope, i'am searching for an algorithm + i'am not allowed to use vectors. – Filip Gorgievski Sep 16 '20 at 14:38
  • 1
    The answer there that uses [`std::sort`](https://en.cppreference.com/w/cpp/algorithm/sort) will work for arrays as well. Also there are a million and one sorting algorithms, so I'm not exactly sure what you're asking for here. I'd suggest [Bogosort](https://en.wikipedia.org/wiki/Bogosort). – scohe001 Sep 16 '20 at 14:42

1 Answers1

0

You can use std::sort with a custom comparator, as follows

std::sort(ponudi, ponudi + brEl,
    [](const auto & lhs, const auto & rhs )
        { return lhs -> getPrice() < rhs -> getPrice();});

asmmo
  • 6,922
  • 1
  • 11
  • 25
  • `begin` and `end` won't work as OP's array is hidden behind a pointer + length. `ponudi, ponudi + brEl` will work, though. – Quentin Sep 16 '20 at 14:44