The District’s homeless shelter is seen at what once was D.C. General hospital. (Bill O'Leary/The Washington Post)

A D.C. Superior Court judge on Monday issued a stinging rebuke of Mayor Vincent C. Gray’s handling of a prolonged surge in family homelessness, ordering his administration to immediately stop housing poor families on cots in gymnasiums on freezing nights.

Judge Robert D. Okun said the communal sleeping quarters that the District began erecting in recreation centers after a midwinter change in policy appeared to deny scores of parents and children their right to privacy and security under city law and may be traumatizing for children.

The temporary injunction forces the District to stop using the makeshift shelters while a class-action lawsuit against the city by 79 homeless families plays out. Okun’s action amounts to the fourth and largest courtroom defeat for Gray’s administration on the controversial shelters — and will dictate for the remainder of the cold weather season how the city provides emergency services for the homeless during hypothermia alerts.

In fact, on the first day of a March cold snap, officials said the judge’s order would probably mean that any family seeking shelter in coming nights would be placed in motel rooms at city expense.

They would join the nearly 400 families who sought shelter earlier in the winter and remain in motels at an ongoing cost to the District — as well as mounting political expense to Gray.

Aside from an ongoing federal investigation into whether Gray first won office with the help of widespread illicit campaign spending, the homeless issue has emerged as the biggest flash point in the mayor’s race. The mayor’s challengers in the April 1 Democratic primary have seized on the record influx of homeless families, and their scattering in motel rooms and rec centers, as evidence of a failed approach to homelessness under the current administration.

The District is one of just a handful of places nationwide with a legal responsibility to shelter the homeless on freezing nights. The law spells out that families should have apartment-style housing or private rooms. In January, as an increasing number of homeless families sought shelter, the city began using recreation centers, using partitions to separate sleeping spaces between families.

Attorneys for Gray’s administration said they would appeal the decision. In a statement, Dora Taylor, a spokeswoman for Gray’s Department of Human Services, forcefully disagreed with Okun’s order. The D.C. Court of Appeals on Monday denied the city’s emergency motion to postpone the judge’s order.

“Unfortunately, we think the judge is simply wrong. The law does not require the District to provide apartment style shelter or private rooms,” Taylor wrote. “Certainly we strive to provide the best possible environment for families, as evidenced by the approximately 800 or more families that we have placed at our apartment style shelters, the private rooms at the DC General family shelter, and over 470 hotel rooms, until we exhausted all of these shelter and hotel rooms available to us.”

Okun rejected the District’s argument that finding private sleeping quarters for any homeless families who seek shelter is a financial burden for a city that has already spent far more than anticipated this winter to shelter families in need. A city lawyer said in court Friday that the District has paid more than $4.5 million this season; $3.2 million was budgeted.

But Okun said in court that the “psychological harm of the most vulnerable members of our society, the children of the homeless,” was greater than the “modest” financial challenges the District may face in finding motels or apartment-style rooms for these families on the remaining frigid nights in the coming weeks.

Washington has experienced a 135 percent increase in homeless families seeking emergency shelter this winter.

The District’s only shelter for children was filled with 300 families at the start of winter. As more families arrived on freezing nights saying they had nowhere to stay, the District was forced to rent more than 400 motel rooms across the city and Maryland. In January, Maryland officials objected to relocating any more homeless families into the state, and District officials said they had no other choice but to take the unprecedented step of putting families in the common rooms of city recreation centers.

The shift to communal shelters has also struck many low-income, homeless and child advocates as callous, drawing in pro bono work by lawyers who this month won class- action status for the 79 families who had been placed in recreation centers since late January.

The lawsuit alleged that children, parents and sometimes grandparents had been unable to shower for days and got only cots in big, noisy rooms that were illuminated all night. Flimsy partitions exposed unrelated families to one another.

The lead plaintiff, Melvern Reid, who has custody of her 10-year-old grandson, described two nights in one of the shelters in which the two could barely sleep and the boy was “terrified.” “He was scared of the strangers in the room with him and did not want to leave his grandmother’s side,” according to the complaint.

The filing said the boy slept in his clothes, afraid to change in front of others; had to be left with a stranger while Reid, 59, used the bathroom; and slept with a blanket over his head because of the lights.

Families who testified last week offered similar stories.

Dalanda Griffin, her husband and her three young children had been doubled up, sharing the apartment of a cousin, in violation of his rental agreement. With nowhere else to go, they turned to the city last week and were placed in a Southeast Washington recreation center that had been turned into a makeshift shelter.

Griffin said her children — ages 2, 4 and 5 — were awake the entire night, with the smell of marijuana wafting through the shelter. They were afraid, she said, of the strangers sleeping in cots near them, separated by six-foot-high partitions. “I didn’t sleep. My children didn’t sleep. We didn’t feel safe,” Griffin, 22, testified.

Griffin testified that one of her children was so afraid at the recreation center that she urinated on herself during the night. She said that people were smoking marijuana outside and that the smoke could be smelled inside. The next night, instead of returning, Griffin and her family slept in the hallway of an apartment building, she said.

Under an earlier court ruling, the city was required to relocate a handful of named plaintiffs in the case to motels.

Outside the courtroom Monday, several of the homeless families hugged the pro bono lawyers with the firm Hogan Lovells. Reid said she stayed in a hotel Sunday night as opposed to returning to the recreation center with her grandson.

“I am so happy,” she said. “I hope everything now works out.”

The District’s long-standing practice had been to allow families put up in motels on hypothermia nights to stay until transitional housing could be provided by the government — usually an apartment subsidized in four-month intervals. Under the city’s January policy change, those seeking housing on freezing nights are sheltered on a night-by-night basis.