diff --git a/partitura/io/exportmusicxml.py b/partitura/io/exportmusicxml.py index 9e349abd..e87f15ca 100644 --- a/partitura/io/exportmusicxml.py +++ b/partitura/io/exportmusicxml.py @@ -417,9 +417,10 @@ def linearize_segment_contents(part, start, end, state): for voice in sorted(notes_by_voice.keys()): voice_notes = notes_by_voice[voice] - # sort by pitch + # sort by pitch (then step in case of enharmonic notes) voice_notes.sort( - key=lambda n: n.midi_pitch if hasattr(n, "midi_pitch") else -1, reverse=True + key=lambda n: (n.midi_pitch, n.step) if hasattr(n, "midi_pitch") else (-1, ""), + reverse=True, ) # grace notes should precede other notes at the same onset voice_notes.sort(key=lambda n: not isinstance(n, score.GraceNote))