@1181631986 – as with most “can I do X in P4” kind of questions, it is extremely important to mention the architecture you plan to use for your program.
For example, some P4_16 architectures (T2NA) do provide specialized mechanisms that allow the packets to be stored in manually controlled queues. By that I mean that individual packets are allowed to turn scheduling for these queues on and off (so-called XON/XOFF operations). Furthermore, the packets in the data plane can specify how many packets can leave the queue once it has been XON-ed. You can find the details in this presentation, slide 7.
In this case case you can use two queues: Q1 will be used to store the packet number 1 and Q2 will be used to store packets 2…5. Q1 will initially be XOFFed, meaning that packets sent to this queue will not be scheduled to be sent out. Q2 will work normally. Once packet number 5 arrives, it will signal Q1 that it can schedule one packet. As long as Q2 has scheduling priority higher than Q1, your packets will be sent out exactly in the order you like.
Please, see this post if you have further questions.
Happy hacking,
Vladimir