The key irony is that Soapy, the main character, is trying to get thrown into jail so that he can have a warm place to stay but cannot seem to get arrested for a series of attempts at being a minor criminal. Soapy hears an anthem coming from a church that reminds him of his better days and decides not to rely on the charity of the police and a warm jail cell, but to turn himself around to become a productive citizen. It is at that precise moment he is arrested for loitering by the church. Just as Soapy decides he doesn't want to be arrested after all, he is arrested. The minor ironies occur throughout the story. He thinks if he goes into a restaurant and orders a big meal, then doesn't pay, he'll get arrested. However, he is turned away from the restaurant before he can order. Next, he goes to shop and throws a rock through the window, then waits for a policeman. When the cop shows up, the cop doesn't believe that a man guilty of the crime would just stand there and wait to be arrested so he doesn't think Soapy did it. When Soapy accosts a woman, thinking he'll get arrested for the things he says to her, she turns out to be a prostitute who hears his words as a business proposition. When he tries to steal a man's umbrella, it turns out that the man had stolen the umbrella himself and thinks Soapy is the rightful owner. When he tries to act like he is drunk, someone thinks he's just being a prankster. He doesn't get arrested until he no longer wants to be arrested.
In O Henry's "The Cop and the Anthem", we have Soapy, our resident ne'er-do-well looking to get a stay on Riker's Island (New York City jail) for the winter. In his endeavors to be incarcerrated, we see him do a number of illegal activities, never terribly destructive to be consistent with his nature. Try as he might, he just can't get arrested and thus does not get his desired room and board for the winter.
Then comes the anthem. A church plays a hymn that takes Soapy to a spiritual epiphany and he upbraiads himself for his unscrupulous ways and decides to live a life that will see him as a productive citizen. The irony comes that just when Soapy decided to turn his life around, the cop decided that the bum had lingered outside the church long enough and charge him with vagrancy. Soapy is sent to jail for three months. Irony often imitates life as it does here.
that the opposit things happen at the list expected time