def sort_events(event):
    """
    .. py:function:: sort_events(event)

        The key function used to sort events (both MIDI and Generic)

        :param event: An object of type :class:`MIDIEvent` or (a derrivative)
            :class:`GenericEvent`

        This function should be provided as the ``key`` for both
        ``list.sort()`` and ``sorted()``. By using it sorting will be as
        follows:

        * Events are ordered in time. An event that takes place earlier will
          appear earlier
        * If two events happen at the same time, the secondary sort key is
          ``ord``. Thus a class of events can be processed earlier than
          another. One place this is used in the code is to make sure that note
          off events are processed before note on events.
        * If time and ordinality are the same, they are sorted in the order in
          which they were originally added to the list. Thus, for example, if
          one is making an RPN call one can specify the controller change
          events in the proper order and be sure that they will end up in the
          file that way.
    """
    return event.time, event.ord, event.insertion_order
