Hi guys, I am creating a 2D casual game and i encounter a problem.
The problem is that I have a highscore features that saves to a text file by storing the name and score
Now i wanted it to sort using bubblesort and structures. How can i achieve that?
Thanks.

#pragma once

class HighscoreSort
{
public:
    HighscoreSort(void);
    ~HighscoreSort(void);

    void bubbleSort(int data[], int length);
};





#include "HighscoreSort.h"

HighscoreSort::HighscoreSort(void)
{
}

HighscoreSort::~HighscoreSort(void)
{
}

void HighscoreSort::bubbleSort(int data[], int length)
{
    for(int iter = 1; iter < length; iter++)
    {
        for(int index = 0; index < length - iter; index++)
        {
            if(data[index] < data[index+1])
            {
                //swap around
                int temp = data[index];
                data[index] = data[index+1];
                data[index+1] = temp;
            }
        }
    }
}

From what I know struct shld be using string name; string score; ???

Edited 3 Years Ago by pritaeas: Moved to C++.

I don't think you have posted this in the right place. I will flag it so it can be moved to the right forum.

Now i wanted it to sort using bubblesort and structures.

Why? Unless you have a very goood reason not to, you should really use std::sort. And even if you do have a very good reason, I don't see why you'd want to use bubble sort instead.

I also don't see why you put your sort function in a class if the class doesn't actually contain anything.

What sepp2k said, with an addendum: if you have a name + score, and you want it sorted by name, then use the std::map template class. Assuming name is a string, and score is an integer, you would do this:

std::map<string,int> scores;
for (size_t i = 0; i < maxscores; i++)
{
    scores[name[i]] = score[i];
    // This assumes that you have two arrays,
    // one with the name, and the other with the score for that name.
}

The std::map<KeyType,ValueType> class will automatically sort the items inserted on the first value (name in this case). If you may have multiple scores for a single name, then use std::multimap<K,V> instead.

This question has already been answered. Start a new discussion instead.