-1

So how can I do this ? I tried with friend functions, overloading operators and can't seem to get it done .

class shop {
 int price;
 string name;
 string model;
 public:
    shop () {
        price =0;
        name = " NULL" ;
        model = " NULL " ;
    }

    shop ( string n , string m , int p ) {
        name = n;
        model = m;
        price = p;
    }

    void display () {
        cout<<"Name : " << name <<endl;
        cout<<"Model : "<<model<<endl;
        cout<<"Price : "<<price<<endl;
    }   
};

How can I sort this by price ?

for ( int i = 1 ; i<=products ; i++) {
    cin.ignore();
    cout<<"Name  "<< i << ": ";
    getline(cin,n); 
    cout<<"Model   "<< i << ": ";
    cin>>m;
    cout<<"Price "<< i << ": ";
    cin>>p[i];

    e[i] =  new shop(n,m,p[i]);
}

I have this vector which I tried sorting. I want to do it without sort as I don't think I declared the vector properly for this method.

jodag
  • 19,885
  • 5
  • 47
  • 66
sebbyz
  • 11
  • 1
  • 1
  • 3
  • 2
    You can just [`std::sort`](http://en.cppreference.com/w/cpp/algorithm/sort) with a custom comparator. – François Andrieux Jan 15 '18 at 20:52
  • 1
    Possible duplicate of [Sorting a vector of custom objects](https://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects) – François Andrieux Jan 15 '18 at 20:52
  • When you say *without "sort"* in the title, which *"sort"* are you talking about? – R Sahu Jan 15 '18 at 20:54
  • 1
    There are many [sorting algorithms](https://en.wikipedia.org/wiki/Sorting_algorithm) out there, although I can't see why you would want to write your own implementation unless it's for learning purposes. – jodag Jan 15 '18 at 20:54
  • 3
    How to walk without legs? How to run programs without computer? How to sort without `std::sort`? Those questions are not useful. Just use the tool that was made specifically to solve your problem. – nwp Jan 15 '18 at 20:54
  • `e[i] =` most likely will access your vector out of bounds. –  Jan 15 '18 at 20:55
  • @manni66 I don't know how you can say that since he didn't post the declaration of `e`. – jodag Jan 15 '18 at 20:59
  • 2
    There are a plethora of sorting examples using an array. The vector can use the array notation to access the elements. If you don't want to use the default `std::sort`, or `std::sort` with custom comparator, you can use `qsort` or write your own. – Thomas Matthews Jan 15 '18 at 21:06
  • @jodag do you really expect that he got it right? –  Jan 15 '18 at 21:40
  • 1
    @jodag, manni66 can make a pretty good guess based on `for ( int i = 1 ; i<=products ; i++)`. sebbyz may have allocated an extra empty slot at the beginning of the `vector`, but it is much more likely that this is an off-by-one bug. Sebbyz, vectors and arrays are origin 0. They start counting at 0, not 1. This means `p[products]` is probably out of range if you haven't taken origin zero into account. – user4581301 Jan 15 '18 at 21:43
  • @ThomasMatthews `qsort` isn't going to work for non-POD types. – PaulMcKenzie Jan 15 '18 at 23:13
  • @PaulMcKenzie - Well, non-trivial types. They don't have to be standard layout for `qsort` to work. – StoryTeller - Unslander Monica Jan 16 '18 at 08:10
  • Why *not* use `std::sort`? – Jesper Juhl Oct 25 '22 at 17:08
  • "I don't think I declared the vector properly for this method" How did you declare it? is it `e`? You don't need `new` to create objects, so you don't need it to be a `std::vector`, it can be a `std::vector`, and then `std::sort` just needs a comparison, most likely `bool operator<(shop, shop)` – Caleth Feb 23 '23 at 10:06

2 Answers2

0

If you don't want to use any built-in sorting function then you have to implement it manually.

// You can use this bubble sort...

for ( int i = 1 ; i<=products ; i++) {    
    for(int j = 1; j <= (products-i+1); j++){ 
        if(e[i].price > e[j].price){
              swap(e[i], e[j]);
        }
    }
}

Its complexity is O(N^2). It takes much time when a number of the product is huge. You can use other algorithm with complexity O(Nlog(N)) such as Merge Sort, Quick Sort etc. to make it faster.

0

This is the way to sort a vector with 2 for:

for (int i = 0; i < vec.size(); i++) {
    for (int j = i; j < (vec.size()); j++) {
        if (vec[i] > vec[j]) {
            std::swap(vec[i], vec[j]);
        }
    }
}
Shark
  • 1