It was originally in alphabetical order but as more was learned about stars a number of letters were dropped and the remaining letters reordered in a more logical order and more was learned about stellar compositions and spectra.
There’s a yet cooler spectral type Y, used for sub-brown dwarfs. So let’s modify this:
Oh, be a fine girl, kiss me like that, Yowza!
More specific answer to the OP. The letters indicate how strong the hydrogen lines in the spectra were. A being the strongest and whatever the highest letter (not sure if they went past O or not) being non-existent. They rearranged them to correspond with temperature when the Hertzsprung-Russell diagram was invented.
IIRC from Astronomy 120 in 1973, the spectral types were based on the strength of certain spectral lines (implying certain relative amounts of elements).
Then it was realized the distribution was Bell-Curve-shaped, So O was the largest stars, as was B, not one of the weakest, and the number of categories was reduced. OBAFGKM
After M I learned of Q (“…Quick”), not L and T or Y. So the list has been in flux over time, as they figure out things like brown dwarfs and such.