Riyad as-Salihin 1871: On Signs of the Last Day etc.
Abu Hurairah relates that the Holy Prophet said: A man made up his mind to give alms. He set out with his alms and put them in the hands of a thief. Next day people began to say: Last night a thief was bestowed alms. The man supplicated: Allah, to Thee belongs all Praise. I shall now give alms again. He went out with his alms and put them in the hands of an adulteress. Next day the people talked: Last night alms were bestowed on an adulteress. The man supplicated: Allah, I praise Thee for enabling me to bestow alms even on an adulteress and I shall give alms once more. He went out with his alms and put them in the hands of a rich man. Next day the people talked: Last night alms were bestowed upon a rich man. The man supplicated: Allah, all praise is Thine, in respect of the thief and the adulteress and the rich man. He was told in his dream: Your alms to the thief might persuade the thief to stop stealing, and in consequence of your alms the adulteress might give up her misconduct and the rich man might draw a lesson from your alms and might start spending out of that which Allah has bestowed upon him (Bokhari).