Sorting array of pointers

Hello,
it would be nice if somebody can help me.
the problem seems to be trivial, but I don't see my mistake.
There is class colony- an array of pointers to objects named bunny. The bunny has the integer method: bunny.age.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class bunny
{
    public:
            int age,s;
            char *sex,*name,*color,*mtn;
            bool mutant;

            bunny();
            void show_bunny();
};
void bunny::show_bunny()
{
    cout<<name<<":  " <<sex<<",  "<<age<<" year(s), "<<color<<".   "<<mtn<<"\n";
}
 class class_colony
{
    private:
        bunny *colony[colony_size];
        int population;//current population of the colony
    public:
        class_colony(int start_population);
        void show_colony();  // Prints all the colony members
};


Within the method show_colony(), I try to sort bunnies by age and print them consequently:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void class_colony::show_colony()
{
    for(int i=0;i<(population-1);i++)
    {
        if(colony[i]->age>colony[i+1]->age)
        {
            bunny *temp;
            temp=colony[i];
            (colony[i])=(colony[i+1]);
            (colony[i+1])=temp;
            //delete temp;
        }
    }
for(int i=0;i<population;i++)
    {
        colony[i]->show_bunny();

    }
    cout<<"Current population: "<<population<<endl;
}

However, I get no sorting at all. What is wrong?
Your sorting algorithm looks like it won't work. At best, it might change around a few bunnies.

Looks like you've tried to implement a bubble sort, but you've missed a key point. With this sort, you have to repeat the sort over the entire array over and over until you find there are no more changes being made.
Your problems begin with declaring strings as char* instead of std::string

Also, use a container rather than using an array directly, use std::vector as a default. For example:
 
    std::vector<bunny> colony;


Can you now see that you don't need to deal with pointers?

Now that your bunnies in a standard container, we can use a standard algorithm to sort them.
Check it out: http://en.cppreference.com/w/cpp/algorithm/sort
Moschops, Tomas, thank you! Indeed, the problem was in incorrect bubble sorting.
Topic archived. No new replies allowed.