Sort per-page MediaSearch by index before adding to image suggestions response data.
Results from the Action API are not sorted in the same order as they are if you perform the same search in MediaSearch on Commons. Looking at the index values of the data from the Action API, the Image Suggestions API is receiving the top X results, but the results are not sorted by index.
Inspecting the MediaSearch extension code, in file SpecialMediaSearch.php function search(), the extension sorts the returned results by index:
We therefore need to mirror this logic by adding an equivalent sort to the Image Suggestion API code. As these are tiny arrays, this will not measurably affect performance.