(hotest) O B A F G K M (coolest)
The reason for the odd arrangement of letters is historical. When people first started taking spectra of stars, then noticed that stars had very different hydrogen line strengths, and so they classified stars on based on the strength of the hydrogen line from A (strongest) to Q (weakest). Latter it was found that some of the classes were actually duplicates and those classes were removed. It was only much later that it was discovered that the strength of the hydrogen line was connected with the surface temperature of the star.