Hacker News new | past | comments | ask | show | jobs | submit login

sorted() will return a new collection. You still need to do foo.sort() in Python if you want to sort in-place.



Don't mind me, just adding the equivalent ruby code for readers to compare:

    array.sort # returns a new collection

    array.sort! # sorts in place
(Ruby convention is to add a ! when methods directly modify the object they're called on.)


> Ruby convention is to add a ! when methods directly modify the object they're called on

No, Ruby convention is to add a “!” when there is a “less safe” version of an operation, for which a “more safe” version exists without a “!”.

Mutating versions of methods where a nonmutating version also exists are one common example, but not the only one, and mutating methods without a nonmutating counterpart don't get “!”.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: