Abu Hurairah (May Allah be pleased with him) said:
The Messenger of Allah (ﷺ) said: "A man (from amongst the people before you) said: 'Indeed! I will give in charity.' So he took his Sadaqah out and placed it in a thief's hand. In the morning the people were talking (about this incident) and saying: 'Sadaqah was given to a thief last night.' The man said: 'O Allah! Praise be to You. I have given Sadaqah to a thief. Indeed, I will give in charity!' So he took his Sadaqah out and he placed it in a prostitute's hand. In the morning the people were talking (about this incident) and saying: 'Sadaqah was given to a prostitute last night.' On hearing this, the man said: 'Praise be to You, O Allah! I gave Sadaqah to a prostitute. Indeed, I will give in charity!' So he took his Sadaqah out and placed it in a rich man's hand. In the morning the people were talking (about this incident) and saying: 'Sadaqah was given to a rich man last night.' The man said: 'O Allah! Praise be to You (for helping me) give charity to a thief, a prostitute and a rich man.' Then he had a dream in which he was told that his Sadaqah to the thief might result in his refraining from his theft, his Sadaqah to the prostitute might help her abstain from her immorality, and his Sadaqah to the rich man might help him pay heed and spend from what Allah had bestowed upon him."[Al- Bukhari].
App reference: Book 19, Hadith 58
Arabic/English book reference: Book 19, Hadith 1865