The order of the flats is given by the mnemonic "Battle Ends And Down Goes Charles' Father", and the order of sharps by the reverse ("Father Charles Goes Down And Ends Battle"). What is the history behind this strange phrase?
From J.R.'s answer on the English Language and Usage Stack Exchange site it dates back at least as far as 1885 since J.R. unearthed a charming quote from Educational Plans in Music Teaching, in The Quarterly Music Review, Vol. 1, 1885 where it is attributed it to a "government schoolmistress".
Unlike other mnemonics for key signature layouts such as BEADGCF ( bead + Greatest Common Factor) or for sharps, fat cats go dancing at elegant balls, the mnemonic for flats, Battle Ends And Down Goes Charles' Father, can be reversed as "Father Charles Goes Down And Ends Battle" to give the order of sharps. That doesn't help with the etymology, but it explains why it has endured despite its awkwardness.