Checking note-offs in a list of note-ons and note-offs

From this list I need to know wich note-ons in this list (notes with a velocity value higher then 0) did not receive a corresponding note-off (a note with a velocity value of 0). Those note-ons should be placed at the very end of this list in the order they occur in the first list.

Can’t figure out how to make a system to do this nice and efficient, the system would also have to be capable of doing this within 10 milliseconds.