logic to find lowest two values

Hello guys,
I am working on a project where I need to compare the current consumed by 4 loads and turn off the two loads which are consuming the least current, i.e. least and second least current consuming loads should be turned off.

I have written a logic for determining the least current, How do I determining the second least value?
here is the code

if (CTA < CTB && CTA < CTC && CTA < CTD)
  {
    Serial.println("CTA in the lowest, turning off room1");
    digitalWrite(room1, LOW);
  }
  else if (CTB < CTA && CTB < CTC && CTB < CTD)
  {
    Serial.println("CTB is the lowest, turning off room2");
    digitalWrite(room2, LOW);
  }
  else if (CTC < CTA && CTC < CTB && CTC < CTD)
  {
    Serial.println("CTC is the lowest, turning off room3");
    digitalWrite(room3, LOW);
  }
  else if (CTD < CTA && CTD < CTB && CTD < CTC)
  {
    Serial.println("CTB is the lowest, turning off room2");
    digitalWrite(room4, LOW);
  }

please suggest a way I can accomplish the above task.

How about sorting the values into order then picking the top two ?

but then after sorting I will only get the sorted values. Not the variables associated with those values.

Not the variables associated with those values.

If you were using arrays, you'd have the indices for the lowest two values. Those same indices would work in the other arrays.

Otherwise, you need to find the smallest value. Then, find the smallest value in the set that does not contain the value/variable that you already found. And THAT is a lot more difficult.

shivakumarj1995:
but then after sorting I will only get the sorted values. Not the variables associated with those values.

If the array is a list of pointers sorted by their values, then you can have both.

Define a struct that contains the room pin and whatever information you need to get the temperature, for simplicity, the temperature too.

Create a four element array of these structs and populate pin and get temperature info.

Iterate through the array and get temperature for each element.

Sort the array on temperature

Turn off the pins in the room in the two low array elements.