# Help for sorting cars that are 1 lap behind the others (latecomers)

Think of 22 cars running in circuit.

I can sort them based on the order they reach the finish line.

I start to have problems, because over time, the faster cars start to reach the slower cars.

Making the cars slower latecomers/laggards/lapped (blue flag in F1).

Event (collision car with finish line):

other.position[other.collision]=pilot;
other.lap[other.collision]=timeLap;
other.timeTotal[other.collision]=timeTotal;
other.collision+=1;

if(other.collision>22){
other.collision=1;
}


OBS - The collision event (shown above) is performed on the car. Every variable (other.) Is because it belongs to the finish line.

I use a variable that serves to define which array index will be defined (other.collision).

Draw Event (Finish Line):

repeat(22){
draw_text(1280,100+(20*(i-1)),string(position[i]));
draw_text(1430,100+(20*(i-1)),string(lap[i]));
draw_text(1530,100+(20*(i-1)),string(timeTotal[i]));
i+=1;
}

i=1;


What happens is that when the first car overcomes the last one, it ends up having the index 22. Becoming the last and vice versa.

I tried to use the time, but the faster car does more turns in less time. Time equals or does not work correctly. So I would also have to use the amount of laps.

That's when I got completely confused.

I am trying to use arrays to order them with the bubble sort, but I didn't get anything functional..

I tried a few more things, but I got lost and I don't even know how to explain it to you.

• I didn't add any more codes because they are practically unreadable (which only I understand, even because I speak another language), so I translate them if necessary so I supplement the question. – Boneco Sinforoso Oct 4 '19 at 1:21

Add a per car lap counter that increments when they hit the finish line.

That way you can sort the cars by that lapcount before you sort those with the same lapcount by their position on the track.

To avoid people being able to simply back up and crossing again and again to increment their lap count, you can put invisible checkpoints that all need to be passed in sequence before the lap counter can increment. Which means you have a nextCheckpoint per vehicle as well.

• Your answer helped me find a solution to my problem. Using a counter for each driver's lap and one more time comparison I got it. – Boneco Sinforoso Oct 8 '19 at 0:55

The ratchet freak's answer gave me the confidence to continue in the code. It is obvious that I needed to correct some things that I had done wrong.

It took me a while to post because I was trying to adapt the code to English.

In the code below you will see that I used counters (repeat), arrays (variable[]) and also the principle of the bubble sort algorithm (link).

Event (collision car with finish line):

laps+=1;
timeTotal+=timeLap;

other.driver[car]=driver;
other.laps[car]=laps;
other.lastLap[car]=timeLap;
other.timeTotal[car]=timeTotal;

repeat(22){
i+=1;

other.ordemDriver[i]=other.driver[i];
other.ordemLaps[i]=other.laps[i];
other.ordemLastLap[i]=other.lastLap[i];
other.ordemTimeTotal[i]=other.timeTotal[i];
}

i=0;

repeat(21){
repeat(21){
if(k<23){
if(other.ordemLaps[j]<other.ordemLaps[k]){
other.auxOrdemDriver=other.ordemDriver[j];
other.ordemDriver[j]=other.ordemDriver[k];
other.ordemDriver[k]=other.auxOrdemDriver;

other.auxOrdemLaps=other.ordemLaps[j];
other.ordemLaps[j]=other.ordemLaps[k];
other.ordemLaps[k]=other.auxOrdemLaps;

other.auxOrdemLastLap=other.ordemLastLap[j];
other.ordemLastLap[j]=other.ordemLastLap[k];
other.ordemLastLap[k]=other.auxOrdemLastLap;

other.auxOrdemTimeTotal=other.ordemTimeTotal[j];
other.ordemTimeTotal[j]=other.ordemTimeTotal[k];
other.ordemTimeTotal[k]=other.auxOrdemTimeTotal;
}
}
k+=1;
}
j+=1;
k=j+1;
}

j=1;
k=2;

repeat(21){
repeat(21){
if(k<23){
if(other.ordemLaps[j]==other.ordemLaps[k]){
if(other.ordemTimeTotal[j]>other.ordemTimeTotal[k]){
other.auxOrdemDriver=other.ordemDriver[j];
other.ordemDriver[j]=other.ordemDriver[k];
other.ordemDriver[k]=other.auxOrdemDriver;

other.auxOrdemLaps=other.ordemLaps[j];
other.ordemLaps[j]=other.ordemLaps[k];
other.ordemLaps[k]=other.auxOrdemLaps;

other.auxOrdemLastLap=other.ordemLastLap[j];
other.ordemLastLap[j]=other.ordemLastLap[k];
other.ordemLastLap[k]=other.auxOrdemLastLap;

other.auxOrdemTimeTotal=other.ordemTimeTotal[j];
other.ordemTimeTotal[j]=other.ordemTimeTotal[k];
other.ordemTimeTotal[k]=other.auxOrdemTimeTotal;
}
}
}
k+=1;
}
j+=1;
k=j+1;
}

j=1;
k=2;


In some places you can see that I only repeated 21 times, which means once less than the maximum amount of cars.

If there were 15 cars, in those places it would be 14 times.

Note that you will have your own car variable, containing some information (number of laps, lap time, total lap time) and a copy at the finish line. Where you will actually make the comparison and consequently the classification of positions.

Variables that have (other.) are variables that belong to the finish line.

I had to use the amount of laps more the total laps time to get an accurate result.