Former Sheriff Joe Arpaio committed a crime by defying a court order to stop detaining suspected undocumented immigrants, a judge ruled on Monday, in the latest rebuke for a once-popular politician who was voted out of office last year.

United States District Judge Susan R. Bolton found Mr. Arpaio, 85, guilty of criminal contempt of court, a misdemeanor punishable by up to six months in jail. Mr. Arpaio’s lawyers said he would appeal.

The conviction comes as sheriffs are under pressure to play a major role in the Trump administration’s immigration crackdown. The president has threatened to withhold federal funds from local governments that do not cooperate by holding jail inmates for deportation, and immigrant advocates have pushed back with lawsuits charging that such detentions violate constitutional rights.

Mr. Arpaio served for 24 years as sheriff of Maricopa County, Ariz., which includes Phoenix, building a national reputation for harsh conditions in his county jail, and for his campaign against undocumented immigrants. The criminal charge grew out of a lawsuit filed a decade ago charging that the sheriff’s office regularly violated the rights of Latinos, stopping people based on racial profiling, detaining them based solely on the suspicion that they were in the country illegally, and turning them over to the immigration authorities.