0

I want all the numbers that are shown on the displays to be linked to the names next to them. The sorted numbers should not be shown in the list on the right like right now with the code, but the names (NPC´s and PC - Labels). How does it work?

int iniwerte[7];  // Array for the values by my LCD-displays
   
ui->lcdNumber_initiative_1->display(iniwerte[0]); // Each display got a value

ui->lcdNumber_initiative_2->display(iniwerte[1]);
ui->lcdNumber_initiative_3->display(iniwerte[2]);
ui->lcdNumber_initiative_4->display(iniwerte[3]);
ui->lcdNumber_initiative_5->display(iniwerte[4]);

std::sort(&iniwerte[0], &iniwerte[7], std::greater<int>()); // Sorting values  

ui->reihenfolge->setNum(iniwerte[0]); // Sorted numbers are shown on the labels
ui->reihenfolge_2->setNum(iniwerte[1]);
ui->reihenfolge_3->setNum(iniwerte[2]);
ui->reihenfolge_4->setNum(iniwerte[3]);
ui->reihenfolge_5->setNum(iniwerte[4]);

The right list should show the associated names instead of numbers.

Here is an example

enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Make a [mcve] so that we know what you're talking about.. – Asteroids With Wings Jun 30 '20 at 18:48
  • 4
    Easiest general way is to make a struct that maps the name to the value, and then sort a container or array of the struct. – user4581301 Jun 30 '20 at 18:50
  • So the best solution is to sort a container? Im programming since 3 months and I dont know how exactly, but I try my best. – Justin Fuchs Jun 30 '20 at 18:59
  • *I want all the numbers that are shown on the displays to be linked to the names next to them.* -- In your code, you should have set up a structure that describes this relationship. Right now, you have individual arrays and names, and there is no relationship between the two, making the job much harder. Read up on `struct`, `class`, and `std::map` and `std::unordered_map`. – PaulMcKenzie Jun 30 '20 at 19:32
  • Could you give me a example of this structure and std::map? – Justin Fuchs Jun 30 '20 at 19:41
  • 1
    You have to conceptualize how to lay out the data. You know what pieces are to be associated with each other. This is where you draw the design out, even if you use simple boxes and lines. Once you have a layout, then go back to the language you're using and figure out how to accomplish what you have on paper. Again, it seems you jumped right into coding, without having a plan of how the data will be related to each other. The problem with jumping straight into coding is that you may eventually paint yourself into a corner when you see that the code was the wrong design from the start. – PaulMcKenzie Jun 30 '20 at 20:00
  • `struct Info { std::string name, int data };` -- That is an example of one `Info` struct that contains a name and associated data. You basically "build out" your structs and classes that fit your design. If the data is more than a simple `int`, then you make the data a `struct` itself. But to be honest, all of this is discussed in any good C++ book. – PaulMcKenzie Jun 30 '20 at 20:02

1 Answers1

0
  1. Define the structure.
struct label_val
{
    std::string label;
    int value;
}
  1. Provide a rule that orders this structure by overloading the greater-than operator to compare based on the labels. See What are the basic rules and idioms for operator overloading? for more information and wisdom on operator overloading.
bool operator> (const label_val& lhs,
                const label_val& rhs)
{
    return lhs.label > rhs.label;
}
  1. make a container of label_vals. Using std::array as an example because it's closest to int iniwerte[7];.
std::array<label_val, 7> iniwerte;
  1. Fill iniwerte as best suits your application.
  2. Sort the sucker.
std::sort(iniwerte.begin(), iniwerte.end(), std::greater<int>());
user4581301
  • 33,082
  • 7
  • 33
  • 54