847
Comment: add name of pattern, reformat code and rename some variables
|
969
dict_ explanation
|
Deletions are marked like this. | Additions are marked like this. |
Line 17: | Line 17: |
There are many ways to do this. Here's the fastest way to do it, as it avoids using a custom comparison function, instead using builtin comparisons. This is the ''decorate-sort-undecorate'' (DSU) pattern, or the ''Schwartzian transform'' if you're coming from Perl. | There are many ways to do this. Here's the fastest way to do it, as it avoids using a custom comparison function, instead using builtin comparisons. This is the ''decorate-sort-undecorate'' pattern, or the ''Schwartzian transform'' if you're coming from Perl. |
Line 27: | Line 27: |
== Questions == This a good way? -- LionKimbro |
In case you're wondering, the variable is named `dict_` because we want to have Wiki:MeaningfulNames, but we don't want to shadow the `dict` builtin, so we append an underscore. |
Sorting Lists of Dictionaries
Frequently you want to sort a list of dictionaries, based on some particular key.
For example:
There are many ways to do this. Here's the fastest way to do it, as it avoids using a custom comparison function, instead using builtin comparisons. This is the decorate-sort-undecorate pattern, or the Schwartzian transform if you're coming from Perl.
In case you're wondering, the variable is named dict_ because we want to have MeaningfulNames, but we don't want to shadow the dict builtin, so we append an underscore.