[SOLVED] How to get the smallest value in 2 different array ? logic problem

Hi , i would like to get the position index of 2 array that work together.

but can't wrap my head around the problem. it must be simple but i am stuck in the rabit hole.

I need the smallest value in the first array "YearMonthDay" (CAN OCCUR MANY TIME) THEN i want to get the smallest value of the second arrray but only if the value in the first array is the lower available (ARRAY "HourMinSecond" CAN NEVER BE THE SAME FOR THE SAME DAY).

here is the code so far , still trying solution.

unsigned long YearMonthDay[10]; // can be the same

// can NEVER be the same for a single DAY, 24H formated
unsigned long HourMinSecond[10];

void setup() {

YearMonthDay[0] = 250803; HourMinSecond[0] = 141425;
YearMonthDay[1] = 250803; HourMinSecond[1] = 140540;
YearMonthDay[2] = 250803; HourMinSecond[2] = 140550;
YearMonthDay[3] = 250803; HourMinSecond[3] = 140720;
YearMonthDay[4] = 250803; HourMinSecond[4] = 140820;
YearMonthDay[5] = 250801; HourMinSecond[5] = 141230;
YearMonthDay[6] = 250802; HourMinSecond[6] = 141425;
YearMonthDay[7] = 250803; HourMinSecond[7] = 140530;
YearMonthDay[8] = 250801; HourMinSecond[8] = 141440;
YearMonthDay[9] = 250803; HourMinSecond[9] = 141432;

Serial.begin(115200);

unsigned long LowestDate = 990000; // = 2099 , never gona be a problem
unsigned long LowestTime = 250000; // = 25h , never gona be a problem
byte Index = 99;

for (byte i = 0; i < 10; i++) {
  if (LowestDate >= YearMonthDay[i] && YearMonthDay[i] != 0) {
    LowestDate = YearMonthDay[i];
    Serial.println();
    Serial.print(" LOOP 1 start ");
    Serial.print(YearMonthDay[i]);
    Serial.print(" - ");
    Serial.print(HourMinSecond[i]);
    Serial.print("  index :  ");
    Serial.println(i);
    Index = i;
    for (byte j = 0; j < 10; j++) {
      if (LowestTime >= HourMinSecond[j] && HourMinSecond[j] != 0) {
        LowestTime = HourMinSecond[j];
        Serial.print(" LOOP 2 start ");
        Serial.print(YearMonthDay[j]);
        Serial.print(" - ");
        Serial.print(HourMinSecond[j]);
        Serial.print("  index :  ");
        Serial.println(j);
        Index = j;
      }
    }
  }
}

Serial.println();
Serial.print("data at index ");
Serial.print(Index);
Serial.print(" : ");
Serial.print(YearMonthDay[Index]);
Serial.print(" ");
Serial.println(HourMinSecond[Index]);

// THE EXPECTED RESULT SHOULD BE INDEX = 5
}

void loop() {
  // put your main code here, to run repeatedly:
}

I'm afraid I have no idea what that phrase is intended to mean.

Oh my god , what a stupid (mod edit: expletive deleted) i am ! :rofl:

fixed it for myself :laughing:

for (byte i = 0; i < 10; i++) {
  if (LowestDate >= YearMonthDay[i]) {
    LowestDate = YearMonthDay[i];
    Serial.println();
    Serial.print(" LOOP 1 start ");
    Serial.print(YearMonthDay[i]);
    Serial.print(" - ");
    Serial.print(HourMinSecond[i]);
    Serial.print("  index :  ");
    Serial.println(i);
  }
}
for (byte i = 0; i < 10; i++) {
  if (LowestDate == YearMonthDay[i] && LowestTime >= HourMinSecond[i]) {
    LowestTime = HourMinSecond[i];
    Serial.print(" LOOP 2 start ");
    Serial.print(YearMonthDay[i]);
    Serial.print(" - ");
    Serial.print(HourMinSecond[i]);
    Serial.print("  index :  ");
    Serial.println(i);
    Index = i;
  }
}

I'd find the lowest value in each array. Then search the first array for its lowest value and compare the value in the second array to the second minimum value. If both are minimal then you found one occurrence, else check the next entry in the first array. Like this:

if (array1[index]==min1 && array2[index]==min2) {
  //found match
}
index++;

Oh, you want the earliest date/time? Consider a simpler solution...

unsigned long YearMonthDay[10];  // can be the same

// can NEVER be the same for a single DAY, 24H formated
unsigned long HourMinSecond[10];

void setup() {

   YearMonthDay[0] = 250803;
   HourMinSecond[0] = 141425;
   YearMonthDay[1] = 250803;
   HourMinSecond[1] = 140540;
   YearMonthDay[2] = 250803;
   HourMinSecond[2] = 140550;
   YearMonthDay[3] = 250803;
   HourMinSecond[3] = 140720;
   YearMonthDay[4] = 250803;
   HourMinSecond[4] = 140820;
   YearMonthDay[5] = 250801;
   HourMinSecond[5] = 141230;
   YearMonthDay[6] = 250802;
   HourMinSecond[6] = 141425;
   YearMonthDay[7] = 250803;
   HourMinSecond[7] = 140530;
   YearMonthDay[8] = 250801;
   HourMinSecond[8] = 141440;
   YearMonthDay[9] = 250803;
   HourMinSecond[9] = 141432;

   Serial.begin(115200);

   unsigned long LowestDate = 990000;  // = 2099 , never gona be a problem
   unsigned long LowestTime = 250000;  // = 25h , never gona be a problem
   byte Index = 99;

   for( byte i = 0; i < 10; i++ ) {
      if( YearMonthDay[i] && YearMonthDay[i] < LowestDate ) {
         LowestDate = YearMonthDay[i];
         if( HourMinSecond[i] && HourMinSecond[i] < LowestTime ) {
           LowestTime = HourMinSecond[i];
           Index = i;
         }
      }
   }

   Serial.println();
   Serial.print("data at index ");
   Serial.print(Index);
   Serial.print(" : ");
   Serial.print(YearMonthDay[Index]);
   Serial.print(" ");
   Serial.println(HourMinSecond[Index]);

   // THE EXPECTED RESULT SHOULD BE INDEX = 5
}

void loop() {
   // put your main code here, to run repeatedly:
}

Edit: on a bike ride afterwards it occurred to me that you'd also have to reset the LowestTime each time the LowestDate changed. Ah well. Seems the OP is pleased with his solution anyhow.

with this data set

YearMonthDay[0] = 250803; HourMinSecond[0] = 141425;
YearMonthDay[1] = 250803; HourMinSecond[1] = 140540;
YearMonthDay[2] = 250803; HourMinSecond[2] = 140550;
YearMonthDay[3] = 250803; HourMinSecond[3] = 140720;
YearMonthDay[4] = 250803; HourMinSecond[4] = 140820;
YearMonthDay[5] = 250801; HourMinSecond[5] = 141230;
YearMonthDay[6] = 250802; HourMinSecond[6] = 141425;
YearMonthDay[7] = 250803; HourMinSecond[7] = 140530;
YearMonthDay[8] = 250801; HourMinSecond[8] = 141440;
YearMonthDay[9] = 250803; HourMinSecond[9] = 141432;

this doesn't work , this return Index 3 when it should return 5

I find a solution that worked at post#3
Thanks