sorting problem

Hi,

I can't think and search of a way how to sort my data "m:sec:mm".

void setup() {
Serial.begin(9600);
}
int m[] = {9, 24, 25};
int sec[] = {19, 34, 27};
int mm[] = {39, 46, 50};

void loop(){
    char s1[10];  
    char s2[10];  
    char s3[10];  
    
    sprintf(s1, "%02d:%02d:%02d", m[1], sec[1], mm[1]);      //put m,sec,mm value to s with formating of m:sec:mm
    sprintf(s2, "%02d:%02d:%02d", m[2], sec[2], mm[2]);      //put m,sec,mm value to s with formating of m:sec:mm
    sprintf(s3, "%02d:%02d:%02d", m[3], sec[3], mm[3]);      //put m,sec,mm value to s with formating of m:sec:mm

}

the code above produces:
s1 = 09:19:39
s2 = 24:34:46
s3 = 25:27:50

and i wanted to sort it from highest to lowest.

thanks in advance

    sprintf(s1, "%02d:%02d:%02d", m[1], sec[1], mm[1]);      //put m,sec,mm value to s with formating of m:sec:mm
    sprintf(s2, "%02d:%02d:%02d", m[2], sec[2], mm[2]);      //put m,sec,mm value to s with formating of m:sec:mm
    sprintf(s3, "%02d:%02d:%02d", m[3], sec[3], mm[3]);      //put m,sec,mm value to s with formating of m:sec:mm

Array indices start at 0, not 1.

and i wanted to sort it from highest to lowest.

You want to sort what? The resulting strings?

What have you tried?

i tried using atoi() to convert the string s1,s2,s3 but it returns 24,25,19. which doesn't make sense in my sorting.

here's what i've been working...it stores time for every player, and displays their time at the end of the program, but i wanted to sort the data "m:sec:mm" from highest to lowest then display who has the slowest time to the fastest time.

#include <LiquidCrystal.h>                  //LCD library

LiquidCrystal lcd(3, 5, 9, 10, 11, 12);     //LCD Pin initialization

int numPlayer = 0;                          //variable for number of player
int i = 0;                                  //incrementing variable for saving to array record

void setup() {
  lcd.begin(16,2);                          //sets the display to have 16x2 display
  pinMode(6, INPUT);                        //pin 6 input
  pinMode(7, INPUT);                        //pin 7 input
}

void loop(){

  while (digitalRead(7) == HIGH){           //while pin 7 is high ask for number of players
    lcd.setCursor(0, 0);                    //sets the cursor of the LCD display to 0,0
    lcd.print("# of Players");              //prints the "# of players" on the LCD display
    if (digitalRead(6) == LOW){             //when pin 6 is press do numplayer++
      numPlayer++;                          //increment the number of player
      delay(500);                           //set delay so that pressing 6 will ensure +1 increment
    }
    lcd.setCursor(0, 1);                    //sets the cursor of the LCD display to 0,1
    lcd.print(numPlayer);                   //prints the value of numPlayer on the LCD display
  }
  int mmRecord[numPlayer];                  //create array with size of numplayer
  int secRecord[numPlayer];                 //create array with size of numplayer
  int mRecord[numPlayer];                   //create array with size of numplayer
timeStart:                                  //label where the time starts
  lcd.clear();                              //clears the display
  lcd.setCursor(7, 0);                      //sets the cursor of the LCD display to 0,1
  lcd.print("Lap Time:");                   //prints the "Lap Time:" on the LCD display
  for (int m = 0; m < 60; ++m) {            //minute is initially 0 and increments until m is 60 or greater
    for (int sec = 0; sec < 60; ++sec) {    //second is initially 0 and increments until sec is 60 or greater
      for (int mm = 0; mm < 100; ++mm) {    //milli is initially 0 and increments until mm is 100 or greater
        display_LCD(m,sec,mm);              //call function display_LCD()
        delay(10);                          //set delay(10)

        if (digitalRead(6) == LOW){         //when pressed have to mm,sec,m arrays
          if ( i != numPlayer){             //condition when i is not equal to numplayer do commands below

            mmRecord[i] = mm;               //mm array
            secRecord[i] = sec;             //sec array
            mRecord[i] = m;                 //m array
            i++;                            //i increment
            lcd.clear();                    //clear LCD
            while (digitalRead(7) == HIGH){ //while pin 7 is high do commands below
              lcd.setCursor(0, 0);          
              lcd.print("Click button 2");
              lcd.setCursor(0, 1);
              lcd.print("to Start Next");      
              delay(100);
            }

            goto timeStart;
          }
        }
        if ( i == numPlayer){
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Game Finish");
          lcd.setCursor(0, 1);
          lcd.print("Time Recap");
          delay(2000);
          for (int j = 0 ; j < i ; j++) {
            lcd.clear();

            lcd.setCursor(0, 0);
            lcd.print("Player");

            lcd.setCursor(7, 0);
            lcd.print(j+1); //displays Player #
            display_LCD(mRecord[j],secRecord[j],mmRecord[j]);  //display time for Player #

            delay(3000);
            }
          }
        }
      }
    }
  }
}

void display_LCD(int m, int sec, int mm){        //create function for printing m:sec:mm format
  char s[10];                                    //array for character number for m:sec:mm
  sprintf(s, "%02d:%02d:%02d", m, sec, mm);      //put m,sec,mm value to s with formating of m:sec:mm
  lcd.setCursor(8, 1);                           //sets the cursor of the LCD display to 8,1
  lcd.print(s);                                  //prints the value of s on the LCD display
}

When dates and times need to be sorted they are usually held as YYYYMMDDHHmmSSms and converted to a readable format only when displayed or printed. The format allows natural sorting either high to low or vice versa.

UKHeliBob:
When dates and times need to be sorted they are usually held as YYYYMMDDHHmmSSms and converted to a readable format only when displayed or printed. The format allows natural sorting either high to low or vice versa.

can you explain further, sample codes would be nice...thank you..

i tried using atoi() to convert the string s1,s2,s3 but it returns 24,25,19. which doesn't make sense in my sorting.

Of course, since the parsing stops at the first non-numeric character.

You can sort the data before creating the string(s) or after. Since this code appears to create just one string at a time, sorting afterwards won't work.

You'd then need to sort mRecord, secRecord, and mmRecord before creating the strings. You probably want to create another array to store player number, and sort all 4 arrays together. Sorting mRecord is pretty simple. What you need to remember to do, though, is change all 4 arrays when one pair of values is found to be out of order in mRecord.

Once the data in mRecord is in order (increasing or decreasing as you see fit), then look at records in secRecord that have the same value in mRecord, and sort them, swapping positions in mmRecord and player number, too.

Once the data in mRecord and secRecord are sorted, then look at records in mmRecord that have the same value in secRecord. Sort them, swapping positions in player number, too.

PaulS:

i tried using atoi() to convert the string s1,s2,s3 but it returns 24,25,19. which doesn't make sense in my sorting.

Of course, since the parsing stops at the first non-numeric character.

You can sort the data before creating the string(s) or after. Since this code appears to create just one string at a time, sorting afterwards won't work.

You'd then need to sort mRecord, secRecord, and mmRecord before creating the strings. You probably want to create another array to store player number, and sort all 4 arrays together. Sorting mRecord is pretty simple. What you need to remember to do, though, is change all 4 arrays when one pair of values is found to be out of order in mRecord.

Once the data in mRecord is in order (increasing or decreasing as you see fit), then look at records in secRecord that have the same value in mRecord, and sort them, swapping positions in mmRecord and player number, too.

Once the data in mRecord and secRecord are sorted, then look at records in mmRecord that have the same value in secRecord. Sort them, swapping positions in player number, too.

yes i get the point of this, but the problem is i don't know how to implement it...i don't know how swapping works in C language.

but the problem is i don't know how to implement it

Think about what it means for records to be in order vs. out of order.

If you want largest to smallest, then records are out of order when a smaller value (in (n+1]) follows a larger value (in [n]):

if(mRecord[n+1] < mRecord[n])
{
   // swap the record(s)...
}

i don't know how swapping works

Swapping the values in mRecord only:

   int temp = mRecord[n+1];
   mRecord[n+1] = mRecord[n];
   mRecord[n] = temp;

i don't know how swapping works

Bonus marks for avoiding the temporary variable, and using XOR. :smiley:

Bonus marks for avoiding the temporary variable

Not for me. I like clear and obvious code.

PaulS:

but the problem is i don't know how to implement it

Think about what it means for records to be in order vs. out of order.

If you want largest to smallest, then records are out of order when a smaller value (in (n+1]) follows a larger value (in [n]):

if(mRecord[n+1] < mRecord[n])

{
   // swap the record(s)...
}






> i don't know how swapping works


Swapping the values in mRecord only:


int temp = mRecord[n+1];
   mRecord[n+1] = mRecord[n];
   mRecord[n] = temp;

thanks for the information, I think I know what to do now...

The bonus marks weren't for you, Paul. :wink:

AWOL:

i don't know how swapping works

Bonus marks for avoiding the temporary variable, and using XOR. :smiley:

what do you mean bonus marks, never heard of them

bonus marks are extra credit.

And the C library provides a sorting routine: qsort(). Since you provide the function that does the comparison, the dates can be in any format, and your comparison routine just has to figure it out.

can you explain further, sample codes would be nice...thank you..

What I had in mind was to hold the times in a single array rather than three with the values in the array to be in SSMMMS format, perhaps created by using sprintf and atol to turn the values into longs which would allow the array to be sorted easily.

When a time is required for display or printing it is converted back. Depending on your requirements and how many time you are dealing with this method may or may not be applicable because of the overheads of conversion between the 2 formats.