Okay, I tested it and now I understand ! I was totally unable to figure out how it worked because it's completely different of what I had in mind : you remember works when other orders are given while I remember works when they are ordered. I had not understand it when you explained it in your first comment, I apologise.
What I like in your idea is above all the fact that the back to work button will show up less frequently.
So, thanks again for your help, I attach a new version of the patch. The problem with i0 may not be an error, but a different point of view ; I think we should slice the orderQueue from i (i.e. the first work order) and not from i0.
I fattened a bit backToWork because I think the back-to-work order should replace the queue, not just push work orders at its end.
I also reordered the functions in UnitAI, grouping backToWork with other workorders functions.
Next step : formation orders.