A federal judge in Florida ordered the state on Friday to change the way candidates are listed on election ballots — a decision that Democrats in the crucial swing state say will take away an unfair advantage Republicans have enjoyed for years.

Democrats called the decision “monumental.” The state said it will appeal.

The current law says that whichever party holds the governor’s office can list its candidates first on the ballot in general elections. That hurts an opposing party, U.S. District Court Judge Mark E. Walker said.

He gave Florida Secretary of State Laurel Lee two weeks to tell elections supervisors in all 67 counties that the current “ballot scheme” is unconstitutional. She has three weeks to come up with a new plan.

Walker heard testimony from researchers who said the “primacy effect” is real and can give a candidate a statistical advantage as great as 5.4 percentage points. Many voters tend to vote for the first name on the list of candidates, they said.

Similar lawsuits filed by Democrats earlier this month in Texas, Arizona and Georgia also challenge laws that allow the party holding the governorship to have its candidates come first on general-election ballots.

Walker wrote that evidence shows ballot order makes a difference in elections. And because “elections are a zero-sum game,” any system that favors one party hurts another, which violates the First and 14th amendments of the Constitution, he ruled.

“The . . . issue is whether the Constitution allows a state to put its thumb on the scale and award an electoral advantage to the party in power,” Walker said. “The answer is simple. It does not.”

Even a slight advantage from the primacy effect can make a difference in close races. Donald Trump claimed Florida by only 1.2 percentage points in 2016.

“Races are won by tiny margins these days,” said Stanford University professor and researcher Jon A. Krosnick, who testified before Walker.

“I believe Donald Trump was elected president because of the primacy effect,” Krosnick said late Friday. “In states where he won by margins of less than 2 percent, he was listed first on all the ballots. He gained a small advantage, and our studies on name-order effects in elections show that it was big enough to cause him to win.”

Republicans in Florida argued that because the law has been in effect since 1951 — through both Democratic and Republican administrations — it should stand unchanged.

Lee said in a statement that the state will comply with Walker’s order while appealing his decision.

“Ensuring fair and accurate elections is the first priority of the Department of State, and maintaining clear and uniform ballots across the state is a matter of great significance,” Lee said.

Terrie Rizzo, chairwoman of the Florida Democratic Party, hailed Walker’s ruling.

“This is a huge victory for Florida voters,” Rizzo said in a statement. “This law was another attempt by Florida Republicans to unconstitutionally sway an election — and give Republicans an unfair advantage. In a state where elections are decided by less than half a percentage point, this is a monumental ruling.”

The judge didn’t tell the state how it must order the ballot. Krosnick said Florida would do well to look to another swing state.

“I think the ideal is what Ohio does,” he said. “They rotate the order of candidate names from precinct to precinct. So you’re first in half the precincts, and I’m first in the other half. That works well.”

Richard Winger, a political activist and editor of Ballot Access News, which follows court decisions on ballots, said that Friday’s decision in Florida is important.

“The experts really do agree that it makes a little bit of a difference, but that difference matters,” Winger said, noting that “Florida has had so many extremely close elections.”

While Democrats are suing red states with similar laws, nobody has yet challenged the one in New York.

“I bet Republicans are going to sue New York, because it has the same law,” Winger said. “And Democrats have been winning New York for a long time.”