Sort lines in alphabetic, numeric orders

Hello guys,

First of all I want to apologize for my poor english but I will try to explain my question as best as possible.

So, I have a .txt file which I write info to (Like basketball team and I write: 1.Player number(int) lets say 15 and 2.Average score(float) lets say 12.5) So at the end I have something like this in my .txt file:

Name Surname AvgPoints JerseyNr
Michael Jordan 50 1
Magic Johnson 35.2 2
Shaq Oneal 29 3
LeBron James 35.1 17

So what I want to do now, is to be able to sort this out and print it out according to chosen parameter-(name,surname,points or jersey number) so let's say I choose average points and it sorts out the thing to this:

Michael Jordan 50 1
Magic Johnson 35.2 2
LeBron James 35.1 17
Shaq Oneal 29 3

Same with name, surname and jerseynr. I would upload my code, but its very large because I'm making a big program with many functions and right now it has 297 lines and ints, floats and etc is not in english. I can tell that all the info from players (points, name and etc) is stored in struct array like this:
player[1].name = Michael, player[2].avgpoints = 35.2 and etc.

So maybe someone could give a good hint how to sort it out. Please if you explain try to explain as simple as possible because my english is kind of bad and I dont know many terminologies. Sorry and Thank You. :)
You can do this.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <fstream>

struct players // Given a struct
{
    std::string firstname;
    std::string lastname;
    float averageScore;
    int playerNumber;
};

void sortByJerseyNumber(players playerObject[],int arraySize)
{

    /*-----------------------------------------------------------------------------
     *  Simple sorting that sorts by jersey number in descending order.
     *-----------------------------------------------------------------------------*/
    for(int iter = 1; iter < arraySize; iter++) // Prevents from comparing array with out of bounds elements
    {
        for(int index = 0; index < arraySize-iter; index++)
        {
            if(playerObject[index].playerNumber < playerObject[index+1].playerNumber) // This part should be easy to understand
            {
                players tempStruct = playerObject[index]; // Create a temporary struct to hold information so we can shift current information
                playerObject[index] = playerObject[index+1];
                playerObject[index+1] = tempStruct;
            }
        }
    }
}

int main()
{
    const int arraySize = 4;
    players playerObject[arraySize] = // Initialized some values
    {
        {"Michael", "Jordan", 50, 1},
        {"Magic", "Johnson", 35.2, 2},
        {"Shaq", "Oneal", 29, 3},
        {"Lebron", "James", 35.1, 17}
    };
    sortByJerseyNumber(playerObject, arraySize);
    std::cout << "Sorted by jersey number: \n";
    for(int i = 0; i < 4; i++)
    {
        std::cout << '\t' << playerObject[i].firstname << std::endl;
        std::cout << '\t' << playerObject[i].lastname << std::endl;
        std::cout << '\t' << playerObject[i].averageScore << std::endl;
        std::cout << '\t' << playerObject[i].playerNumber << std::endl << std::endl;
    }
    return 0;
}


This is just for sorting the players in descending jersey number. Can you figure out the functions for the others? (Hint: They are VERY similar).
You do not need to re-invent the wheel by making your own sort method.
Use std::sort in <algorithm> in tandem with custom comparator classes for sorting.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // sort

struct Player {
    
    std::string name;
    double avg_points;
    int jersey_number;

    struct compareByName {
        bool operator()(const Player& a, const Player& b)
        {
            return a.name < b.name;
        }
    };
    
    struct compareByAveragePoints {
        bool operator()(const Player& a, const Player& b)
        {
            return a.avg_points < b.avg_points;
        }
    };
    
    struct compareByJerseyNumber {
        bool operator()(const Player& a, const Player& b)
        {
            return a.jersey_number < b.jersey_number;
        } 
    };
};

void print(const std::vector<Player>& basketball_team, const std::string& sort_method)
{
  std::cout << "Sorted by " << sort_method << ":\n  ";
  for (const auto& player : basketball_team)
      std::cout << player.name << " " << player.avg_points << " " << player.jersey_number << "\n  ";
  std::cout << std::endl;
}

int main()
{

  std::vector<Player> basketball_team {
    { "Michael Jordan", 50,    1 },
    { "Magic Johnson",  35.2,  2 },
    { "Shaq Oneal",     29,    3 },
    { "LeBron James",   35.1, 17 } 
  };
    
  std::sort(basketball_team.begin(), basketball_team.end(), Player::compareByName());
  print(basketball_team, "Name");
  
  std::sort(basketball_team.begin(), basketball_team.end(), Player::compareByAveragePoints());
  print(basketball_team, "Average Points");
  
  std::sort(basketball_team.begin(), basketball_team.end(), Player::compareByJerseyNumber());
  print(basketball_team, "Jersey Number");
   
}


Sorted by Name:
  LeBron James 35.1 17
  Magic Johnson 35.2 2
  Michael Jordan 50 1
  Shaq Oneal 29 3
  
Sorted by Average Points:
  Shaq Oneal 29 3
  LeBron James 35.1 17
  Magic Johnson 35.2 2
  Michael Jordan 50 1
  
Sorted by Jersey Number:
  Michael Jordan 50 1
  Magic Johnson 35.2 2
  Shaq Oneal 29 3
  LeBron James 35.1 17
Last edited on
Wow, it was so SIMPLE! Thank You guys VERY much! :))
Topic archived. No new replies allowed.