The concern I have is a situation where a note on event is followed by some event X followed by a note off event (not for the same note). Is there any scenario where the result would be different if I reorder these events?
I can't immediately think of a situation that would cause a problem. But may I suggest you clearly document what your software is doing, and possibly add some option to disable the re-arrangement processing? If a person authoring a MIDI file really needs to send events in a certain order for some reason, it could be frustrating when the software they are using secretly rearranges things. However, I might be a little biased as a MIDI technical enthusiast, perhaps it's not needed for typical cases and typical users.
You only mentioned re-arranging events of a single track. But I observed a related effect when I was experimenting with
putting notes of the same channel on different tracks (scroll down to "CHANNEL ON MULTIPLE TRACKS"). I inadvertently made a note start and note end for the same pitch and time on different tracks. One of the MIDI players I use cuts the new note that is starting off, presumably because it sends the note start then immediately sends the note end for the same pitch. But other MIDI players I use play the new note fine, and so I guess they must be re-arranging the note end to get sent before the new note start.
So those results I observed seem to imply that some MIDI software might have considered this kind of issue and decided to re-arrange note ends to get sent before note starts of the same tick, channel, and pitch.
If the "Note Off" is for the same note as in the following "Note On", you can (and should) remove the "Note Off".
If they are for different notes, then the order does not matter.
Sema: You shouldn't remove the Note Off. In MIDI, it is important that every note start [Note On with non-zero velocity] should eventually be followed by a note end [Note Off or Note On with zero velocity] for the same pitch.
Some MIDI devices may treat another note start of the same pitch as cutting off the first note of that pitch. But some MIDI devices will add another note of the same pitch to what is already sounding. Because of that, the MIDI specs say you should always send a note ending message for every note starting message. In the
MIDI 1.0 Detailed Specification, PDF page 67 (printed page A-4):
ASSIGNMENT OF NOTE ON/OFF COMMANDS
If an instrument receives two or more Note On messages with the same key number and MIDI channel, it must make a determination of how to handle the additional Note Ons. It is up to the receiver as to whether the same voice or another voice will be sounded, or if the messages will be ignored. The transmitter, however, must send a corresponding Note Off message for every Note On sent. If the transmitter were to send only one Note Off message, and if the receiver in fact assigned the two Note On messages to different voices, then one note would linger. Since there is no harm or negative side effect in sending redundant Note Off messages this is the recommended practice.