snipt

Ctrl+h for KB shortcuts

Python

python sort list of objects by object attribute

ut.sort(key=lamdba x: x.count, reverse=True)
https://snipt.net/embed/d29b96d5c94c5b4591cb02a08d150862/
/raw/d29b96d5c94c5b4591cb02a08d150862/
d29b96d5c94c5b4591cb02a08d150862
python
Python
1
2019-08-24T23:20:11
True
False
False
/api/public/snipt/1423/
python-sort-list-of-objects-by-object-attribute
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">ut</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">lamdba</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">count</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> </span></pre></div> </td></tr></table>
python, sorting