Autocompletion: Fix issue #3723 -- ordering of completions for magic commands and variables with same name

This PR fixes issue #3723.

The introduction of the `%matplotlib` magic broke the order of autocompletions in the sequence of commands described in #3723.

This PR fixes this by changing the order in which the completions are returned by the `complete()` function, by 
introducing a custom key, called `penalize_magics_key`, which is passed to `sorted` to sort the various lists of completions assembled from different places.

`penalize_magics_key` moves the initial `%` or `%%` to the *end* of the key, so that the lexicographic string ordering gives `"matplotlib" < "matplotlib%" < "matplotlib%%"`, but all of these are together in the completion list.

Added a test function `test_magic_completion_order` which tests that these orderings are correct.