def sort(s):
listt = list(s)
listt.sort(cmp=lambda a,b: cmp( ord(b)%len(s) , ord(a)%len(s) ))
print ''.join(listt),
sort('Oh,well')
sort('lord')
And this is the rather succinct Ruby version: def sort s
print s.split(//).sort_by.with_index{|a,i|[-(a.ord%s.length),i]}.join
end
sort 'Oh,well'
sort 'lord'
RiderOfGiraffes lists some relevant HN items here: http://news.ycombinator.com/item?id=1562602