<div dir="ltr"><div>Hi all,</div><div><br></div><div>I have a list called count_list which contains tuples like below:<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">[('bridge', 2), ('fair', 1), ('lady', 1), ('is', 2), ('down', 4), ('london', 2), ('falling', 4), ('my', 1)]</blockquote><div><br></div><div>I want to sort it based on the second parameter in descending order and the tuples with the same second parameter must be sorted based on their first parameter, in alphabetically ascending order. So the ideal output is:<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">[('down', 4), ('falling', 4), ('bridge', 2), ('is', 2), ('london', 2), ('fair', 1), ('lady', 1), ('my', 1)]</blockquote><div><br></div><div><br></div><div>What I ended up doing is:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">count_list = sorted(count_list,<br>                    key=lambda x: (x[1], map(lambda x: -x, map(ord, x[0]))),<br>                    reverse=True)</blockquote><div><br></div><div>which works. Now my solution is very specific to structures like [(str, int)] where all strs are lower case and besides ord makes it to be both limited in use and also more difficult to add extra levels of sorting.</div><div><br></div><div>The main problem is that reverse argument takes only a boolean and applies to the whole list after sorting in finished. I couldn't think of any other way (besides mapping negative to ord values of x[0]) to say reverse on the first level but not reverse on the second level. Something like below would be ideal:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">count_list = sorted(count_list,<br>                    key=lambda x: (x[1], x[0]),<br>                    reverse=(True, False))</blockquote><div><br></div><div>Does such a way similar to above exist? If not, how useful would it be to implement it?</div><div><br></div><div><br></div><div><b>P.S.</b> It's my first time on a mailing list. I apologize before hand if such a thing has already been discussed or even there exist a way which already achieves that.</div></div>