Following the STS-27 flight the Shuttle mission designations got into a mess. The anwer to this question mentions that it was due to schedules being moved around. Now while it wouldn't have hurt to just rename the missions so that the schedule wouldn't look so weird, it can't be the case that missions have been postponed so that another mission came first either (as in the case where Gemini 7 launched before Gemini 6). The Shuttle missions weren't postponed that much that another mission came before, with some exceptions perhaps. Following the Columbia tragedy, they made it so that if a Shuttle mission was postponed all subsequent missions were postponed too, perhaps they did like that already before STS-114. Is there an explanation for why STS-114 was followed by STS-121 and something?
tl;dr The Space Shuttle mission numbers were assigned in numerical order of their planned launch sequence.1 When that sequence changed, the mission numbers were not changed.
The Shuttles and their payloads were assigned their flight designations when the mission was planned, not when the mission would be launched.
Interestingly enough, NASA considered at one point changing the designations as the launch order changed, but officials quickly realized that this would have confused too much of the mission paperwork. Once the flight designation was assigned, it stuck. No more changes could be made. That way, no matter what delays or reschedulings moved the launch around, the designation for a mission would be the same—from the day it was first planned to the day it landed.
1 Before and after the goofy "let's hide our launch scheduling issues by obfuscation" era, that is. See Why so many STS-61's? for more info