WASHINGTON -- The Supreme Court ruled Tuesday that immigrants the government has detained and is considering deporting aren't entitled by law to periodic bond hearings. The case is a class-action lawsuit brought by immigrants who've spent long periods in custody.

The group includes some people facing deportation because they've committed a crime and others who arrived at the border seeking asylum.

The San Francisco-based U.S. Court of Appeals for the 9th Circuit had ruled for the immigrants, saying that under immigration law they had a right to periodic bond hearings. The appeals court said the immigrants generally should get bond hearings after six months in detention, and then every six months if they continue to be held.

Get Breaking News Delivered to Your Inbox

But the Supreme Court reversed that decision Tuesday and sided with the Trump administration, which had argued against the ruling, a position also taken by the Obama administration.

Immigrants wait for medical attention during "sick call" at the Adelanto Detention Facility on Nov. 15, 2013, in Adelanto, California. John Moore/Getty Images

On Monday, President Trump criticized the 9th Circuit after the Supreme Court rejected the administration's highly unusual bid to bypass the appeals court and get the justices to intervene in the fate of an Obama-era program that protects hundreds of thousands of young immigrants from deportation.

"It's really sad when every single case filed against us is in the 9th Circuit, we lose, we lose, we lose, and then we do fine in the Supreme Court," the president said. "But what does that tell you about our court system. It's a very, very sad thing."

In Tuesday's ruling, Justice Samuel Alito wrote for five justices that immigration law doesn't require periodic bond hearings. But the justices sent the case back to the appeals court to consider whether the case should continue as a class action and the immigrants' arguments that the provisions of immigration law they are challenging are unconstitutional.

But Justice Stephen Breyer, writing a dissenting opinion joined by two other liberal-leaning justices on the court, Justice Sonia Sotomayor and Justice Ruth Bader Ginsburg, said he would have read the provisions of immigration law to require hearings for people detained for a prolonged period of time.

"The bail questions before us are technical but at heart they are simple," Breyer wrote. "We need only recall the words of the Declaration of Independence, in particular its insistence that all men and women have 'certain unalienable Rights,' and that among them is the right to 'Liberty,'" he wrote.

The American Civil Liberties Union, which brought the case on behalf of the immigrants, had previously said that about 34,000 immigrants are being detained on any given day in the United States, and 90 percent of immigrants' cases are resolved within six months. But some cases take much longer.

In the case before the justices, Mexican immigrant Alejandro Rodriguez was detained for more than three years without a bond hearing. He was fighting deportation after being convicted of misdemeanor drug possession and joyriding, and was ultimately released and allowed to stay in the United States.

The justices first heard arguments in the case in November 2016, when they were down a member following the death of Justice Antonin Scalia. An eight-justice court was apparently split over the case, however, and the justices re-heard arguments after Justice Neil Gorsuch joined the court. But after those arguments Justice Elena Kagan withdrew from the case, saying her staff had inadvertently overlooked a conflict. That again left eight justices ruling on the case.

The case is Jennings v. Rodriguez, 15-1204.