Sorting variables and print names, not values

So i have:

int sensorA = 300;
int sensorB = 100;
int sensorC = 1;
int sensorD = 489;
int sensorE = 6;

I need to sort them, but i need to print not sorted variables but sensor names, so output shloud be C, E, B, A, D.

Making an array of int and sorting them is easy, but how easy do that?

Once compiled there will be no trace of the variable names in the code. Put the names in an array of chars in the order that you want. Iterate through that and print the names.

I know that,

So there will be two arrays char sensors[6][2] = {"A", "B", "C", "D", "E"} and int sensorvalues[6] = {300,100,1,489,6}, but how pair it in Arduino as std:pair is not avalible.

sirwiz:
I know that,

So there will be two arrays char sensors[6][2] = {"A", "B", "C", "D", "E"} and int sensorvalues[6] = {300,100,1,489,6}, but how pair it in Arduino as std:pair is not avalible.

use a struct

struct Sensor{
  char* friendlyName;
  int value;
};

Sensor sensor[] = {
  {"sensorA", 0},
  {"sensorB", 0},
  {"sensorC", 0},
  {"sensorD", 0},
  {"sensorE", 0},
};

void setup() 
{
  Serial.begin(9600);
  sensor[0].value = 300;
  sensor[1].value = 100;
  sensor[2].value = 1;
  sensor[3].value = 489;
  sensor[4].value = 6;
  
  for (auto i : sensor)
  {
    Serial.print(i.friendlyName);
    Serial.print(F(" = "));
    Serial.println(i.value);
  }
}

void loop() 
{

}

How to sort it without std?

But how to sort it without std?

The same way people sorted arrays before the std library was developed. I'm almost certain that Mr. Google knows about 417 million ways.

Maybe even more.

But for example:

  for (i = 1; i < 6; i++) {
      for (j = 0; j < 6 - 1; j++) {
         if (sensor[j].value > sensor[j + 1].value) { 
            temp = sensor[j].value;
            sensor[j].value = sensor[j + 1].value;
            sensor[j + 1].value = temp;
         }
      }

Will sort only value, and leave friendlyName as it was in rows. Goal is to sort "friendlyName" by "value" value.

You could have an array of values, in name order:

int sensors[5] = {300, 100, 12, 496, 82, 17};
const char* sensor_names[] = {"A", "B", "C", "D", "E"};

Array of indices to particular sensors:

int sortable_indicies[5] = {0, 1, 2, 3, 4};   //before sorting, these just count.

Then, sort the indices using a level of indirection:

  if (sensors[sortable_indices[i]] < sensors[sortable_indices[j]] {
   swap( sortable_indices[i], sensors[sortable_indices[j] );
// eventually giving you a set of sorted indirect indices:
// sortable_indices[] = {2, 4, 3, 1, 0}

And you can print them in sorted order with something like:

    for (byte i=0; i <4; i++) {
       printf("Sensor%s = %d\n", sensor_names[sortable_indicies[i], sensors[sortable_indices[i]);
    }

Thanx, this is idea that i need.

So:

long int sensors[6] = {300, 100, 12, 496, 82, 17};
const char* sensor_names[] = {"A", "B", "C", "D", "E", "F"};
byte sortable_indices[6] = {0, 1, 2, 3, 4, 5};
void sort()
{
int i,j;
long int temp;

for (i = 1; i < 6; i++) {
      for (j = 0; j < 6 - 1; j++) {
         if (sensors[sortable_indices[j+1]] < sensors[sortable_indices[j]]) {
            temp = sortable_indices[j];
            sortable_indices[j] = sortable_indices[j + 1];
            sortable_indices[j + 1] = temp;
         }
      }
 

}
}
for (int i = 0; i < 6; i++) {

Serial.print(sensor_names[sortable_indices[i]]);
Serial.print(" - ");
Serial.println(sensors[sortable_indices[i]]);

}

I hesitate to point out that "A", "B", etc. are lousy sensor names. But, I got over it.

There is really no need to sort the values, to find the largest one. A single pass through the array will find the largest value and it's position in the array. The name is at the same position in the other array.

About sensor names, they are OK for me, as i have stations named A....Z :wink:

Higest yes, but if i want print all values sorted, or get 5th place, sorting is better.