There are some limited circumstances when employers can prefer one sex to another. For example, being a woman might be a bona fide occupational qualification for working as a guard in a female prison. Or being a man might be a bona fide occupational qualification for a particular role in a movie.
Guards live on site in dormitories.
The prison refuses to hire female guards to serve in positions that involve contact with prisoners. In this circumstance, being male is a bona fide occupational qualification for the position of prison guard. Circumstances in which preferences for one sex are allowed are very rare. A female customer at a hotel who requests a massage may prefer a female massage therapist. But a customer's preference for privacy is not a bona fide occupational qualification for the position of massage therapist.