MidiFile changes between write and read

I am creating midi notes via code using keyboardState.noteOn and keyboardState.noteOff messages. I then add timeStamps in the handleNoteOn and handleNoteOff listeners and add them to a midiMessageSequence as follows:

void handleNoteOn (juce::MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity) override
{
     auto m = juce::MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity);
     m.setTimeStamp (juce::Time::getMillisecondCounterHiRes() * 0.001 - startTime);
     midiMessageSequence.addEvent(m);
}

void handleNoteOff (juce::MidiKeyboardState*, int midiChannel, int midiNoteNumber, float /*velocity*/) override
{
     auto m = juce::MidiMessage::noteOff (midiChannel, midiNoteNumber);
     m.setTimeStamp (juce::Time::getMillisecondCounterHiRes() * 0.001 - startTime);
     midiMessageSequence.addEvent(m);
}

When the user hits the stop recording button I make a MidiFile as follows:

midiMessageSequence.updateMatchedPairs();
midiMessageSequence.sort();
MidiFile midiFile;
midiFile.addTrack(midiMessageSequence);
File fileToWrite(parentDir.getFullPathName() + "/" + "test.mid");
std::unique_ptr<FileOutputStream> fileOutput(fileToWrite.createOutputStream());
midiFile.writeTo(*fileOutput);

Immediately before I write the MidiFile the midiMessageSequence looks like this:

midiMessageSequence.getNumEvents(): 8
midiMessageSequence.getStartTime(): 43.2249
midiMessageSequence.getEndTime(): 44.3163
channel: 1 note: 48 velocity: 127 timeStamp: 43.2249
channel: 1 note: 48 velocity: 0 timeStamp: 43.2481
channel: 1 note: 57 velocity: 127 timeStamp: 43.2481
channel: 1 note: 57 velocity: 0 timeStamp: 43.2713
channel: 1 note: 59 velocity: 127 timeStamp: 43.2713
channel: 1 note: 59 velocity: 0 timeStamp: 43.2945
channel: 1 note: 60 velocity: 127 timeStamp: 43.2945
channel: 1 note: 60 velocity: 0 timeStamp: 44.3163

But when I read it back in it looks like this:

midiFile.getNumTracks(): 1
midiFile.getTimeFormat(): -6360
midiFile.getTrack(0)->getNumEvents(): 9
channel: 1 note: 48 velocity: 0 timeStamp: 43
channel: 1 note: 57 velocity: 0 timeStamp: 43
channel: 1 note: 59 velocity: 0 timeStamp: 43
channel: 1 note: 48 velocity: 127 timeStamp: 43
channel: 1 note: 57 velocity: 127 timeStamp: 43
channel: 1 note: 59 velocity: 127 timeStamp: 43
channel: 1 note: 60 velocity: 127 timeStamp: 43
channel: 1 note: 60 velocity: 0 timeStamp: 44
channel: 0 note: 47 velocity: 0 timeStamp: 44

I don’t understand why the order has changed and why the timeStamps now appear to be integers. I also don’t know why there is an additional MidiMessage on channel 0 at the end of the read in file. Any ideas what I am doing wrong?

I figured out how to make the timeStamp properly from this post:

https://forum.juce.com/t/midimessagesequence-midifile-question/20023