Sorting an array of struct objects

I have an array of struct obj and if I do a bubble sort on it in the following way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Friend
{
    std::string name;
    int days;
};

    for(int i=0;i<size;++i)
    {
        for(int j=0;j<size-1;++j)
        {
            if(sorted_f[i].days<sorted_f[j].days)
            {
                Friend temp;
                temp.name = sorted_f[i].name;
                temp.days = sorted_f[i].days;
                sorted_f[i].name = sorted_f[j].name;
                sorted_f[i].days = sorted_f[j].days;
                sorted_f[j].name = temp.name;
                sorted_f[j].days = temp.days;
            }
        }
    } //it sorts that array 


The array is sorted,but if I try this:

1
2
3
4
5
6
7
8
9
10
11
12
  for(int i=0;i<size;++i)
    {
        for(int j=0;j<size-1;++j)
        {
            if(sorted_f[i].days<sorted_f[j].days)
            {
                Friend temp = sorted_f[i];
                sorted_f[i] = sorted_f[j];
                sorted_f[j] = temp;
            }
        }
    } //does not sort the array 


My question is why do I need to specify each element of the array obj when sorting instead of treating the object like a new simple array and sorting it out?
Are you sure? Can you give an example?
 why do I need to specify each element of the array obj when sorting instead of treating the object like a new simple array and sorting it out?

because Friend objects have 2 data-members i.e. 2 dimensions and you need to let the compiler know by which of these dimensions the sorting has to be done. You can also overload the operator < and in that case you can sort the objects outright with the operator < overload already defining on what basis you're looking to sort:
http://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects
Normally you should be able to assign one Friend object to another. What compiler do you use?
Can you show us the whole code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

struct Friend
{
  string name;
  int days;
};

int main()
{
  Friend Anna{ "Anna", 19 }, Lisa{ "Lisa", 20 };

  Friend tmp = Lisa;
  Lisa = Anna;
  Anna = tmp;

  cout << "\nAnna name: " << Anna.name << "\tAnna days: " << Anna.days;
  cout << "\nLisa name: " << Lisa.name << "\tLisa days: " << Lisa.days;

  return 0;
}

OUTPUT

Anna name: Lisa Anna days: 20
Lisa name: Anna Lisa days: 19


@Peter87
I tried again to use the second version and it worked, turns out it was a issue with my display function, so in the end both versions work.
I am curious however: Is there any performance cost between the two?

@gunnerfunner
Thank you this is a very useful hint you pointed out.

@Thomas1965
It works now thank you,there was a problem with another function that displayed the output wrong.
I use the GNU GCC compiler default of code::blocks.
Is there any performance cost between the two?


While we are on the topic, I have another question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Friend_list::add_friend()
{
    system("CLS");
    Friend *f_new=new Friend[++size];
    if(size>=1)
    {
        for(int i=0;i<size-1;++i)
        {
            f_new[i]=f[i];
        }
    }
    f=f_new;
    delete f_new; // if I use delete[] here;crash
    f_new=NULL;
    cout<<"\n\n    \"Add friend\"\n\n";
    cout<<"    Please input name: ";
    cin.ignore();
    getline(cin,f[size-1].name,'\n');
    cout<<"    Please input no. of days: ";
    cin>>f[size-1].days;
    while(f[size-1].days<0)
    {
        cout<<"    Please input no. of days: ";
        cin>>f[size-1].days;
    }
}


I have read about dynamic allocation on my struct as you can see my object is dynamically allocated as an array and I should free it to prevent memory leak,but most people state that a <type> *ptr = new <type>[] should be followed with a delete[] ptr statement not a simple delete ptr.
However if I try doing so to avoid undefined behavior my program crashes, any idea why?
Last edited on
Both f and f_new are pointing to the new array on line 13 so delete f_new will free the new array. To free the old array you should use delete[] f before it points to the new array.

1
2
3
delete[] f;
f=f_new;
delete f_new;
Last edited on
I understand now, so when I use the delete[] f_new, I delete the array pointed to by f making the pointer invalid so the program crashes.
@Peter87 thank you very much for the explanation, much appreciated!
Topic archived. No new replies allowed.