<div><br class="webkit-block-placeholder"></div><div><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Hi</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">I have a hard time sorting an object list. Perhaps this is kind of a noob question, but I would very much appreciate any help!</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Using django I get a QuerySet of Score objects which are sorted by the actual score, the actual score divided by the max. possible score (so sorting by two db fields).
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">I then need to loop through that queryset and sum up all the score objects which belong to the same user into one Score objects. This works (see code below).
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">The problem I now have, is that I lost the sorting order, as described above. How would I resort it with a python algortithm instead of SQL?</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">        </span>scores = Score.objects.order_by(&#39;score&#39;, &#39;score2&#39;,&#39;owner&#39;) # filter by course, MC !!
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><span class="Apple-tab-span" style="white-space:pre">        </span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">        </span># loop through scores to regroup by user</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">        </span>newscore = []</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">        </span>for s in scores:</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                </span>i = False</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                </span># loop through new object container and check for existant user index, add scores if existant
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                </span>for ns in newscore:</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                        </span>if s.owner == ns.owner:</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                                </span>ns.score = int(ns.score) + int(s.score)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                                </span>ns.maxscore = int(ns.maxscore) + int(s.maxscore)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                                </span>i = True</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                </span># otherwise append new user index object, work with it later, perhaps (if more user objects exist)
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                </span>if i == False:</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                        </span>newscore.append(s)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">-----------------</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">I did fiddle around with .sort() but didn&#39;t get any useful results (and it would only sort by one object..).</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">class CmpAttr:</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">        </span>def __init__(self, attr):</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                </span>self.attr = attr</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">        </span>def __call__(self, x, y):</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="Apple-tab-span" style="white-space:pre">                </span>return cmp(getattr(x, self.attr), getattr(y, self.attr))</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">newscore.sort(CmpAttr(&quot;score&quot;))</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br class="webkit-block-placeholder"></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px">ps. could it be, that this maillist is blocking some e-mail addresses?</p>

</div>