Ranks are distinguished by the number of stripes so a Captain comes next but there are many sub-groups in various forces so a Group Commander would outrank a Wing Commander.
For instance, I was a Sub-Lieutenant (the navigator) on my ship which was a repair ship and I was a very lowly rank... but I was 3rd in line of command after the Captain (a Commander). That was because I was a 120 designator officer... I had a license to drive a ship. The technical commander (a Commander with three stripes... I had one stripe) on the ship had no knowledge of how to drive a ship or how to navigate... so in the event the 120 designator officers were out of action, I would have been in command of the ship and had authority over all senior ranks to me on board my ship. But on land the technical superior ranks would outrank me. For officers of the same rank, the one who received their rank first is senior.