<html><body><div style="color:#000; background-color:#fff; font-family:bookman old style, new york, times, serif;font-size:14pt"><div><span>This makes sense.  Thanks.  <br></span></div><br>No question on the specific code, I was just thinking I should show I'd done any experimenting with the methods <br><br><br><div style="font-family: bookman old style, new york, times, serif; font-size: 14pt;"><div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 12pt;"><div class="y_msg_container">Hi Patti,<br clear="none"><br clear="none">My answers below, interleaved between your questions.<br clear="none"><br clear="none">On Tue, Apr 22, 2014 at 04:18:38PM -0700, Patti Scott wrote:<br clear="none"><br clear="none">> I'm practicing with lists.  I was looking for documentation on sorting <br clear="none">> with cmp() because it isn't immediately clear to me how comparing <br
 clear="none">> items two at a time can sort the entire list.  Identify max or min <br clear="none">> values, yes, but not sort the whole list.  So, the Sorting HOW TO <br clear="none">> (Dalke, Hettinger)  posted on python.org goes into detail on using a <br clear="none">> key parameter for sorted() and .sort(), and using operator module <br clear="none">> functions. <br clear="none"><br clear="none">Think about how you might sort four items 42, 23, 57, 30. There are many <br clear="none">different ways to sort, and this is one of the least efficient, but <br clear="none">easiest to understand. We start by putting the unsorted items on the <br clear="none">left, and the sorted items on the right, as if we were sorting a handful <br clear="none">of playing cards:<br clear="none"><br clear="none">[42, 23, 57, 30] []<br clear="none"><br clear="none">Take the first item from the left, and find where it belongs on the <br
 clear="none">right. Since the right is currently empty, that's easy:<br clear="none"><br clear="none">[23, 57, 30] [42]<br clear="none"><br clear="none">Now take the next item from the left, and find where it belongs on the <br clear="none">right. How do you do that? By comparing it to each item already there. <br clear="none">If it compares less than the item, insert it just before the item; <br clear="none">otherwise keep going.<br clear="none"><br clear="none">In this case, we compare 23 < 42, which returns True, so we insert 23 to <br clear="none">the left of 42.<br clear="none"><br clear="none">[57, 30] [23, 42]<br clear="none"><br clear="none">Now repeat with the next item. In this case, 57 < 23 returns False, so <br clear="none">we continue. 57 < 42 also returns False, and there are no more numbers <br clear="none">to check so we put 57 at the end:<br clear="none"><br clear="none">[30] [23, 42, 57]<br clear="none"><br
 clear="none">Finally we compare 30 < 23, which returns False, then 30 < 42, which <br clear="none">returns True, so we insert 30 just to the left of 42:<br clear="none"><br clear="none">[] [23, 30, 42, 57]<br clear="none"><br clear="none"><br clear="none">Now that we know how to sort using "less than" < as the comparison <br clear="none">function, we can use some other comparison function that works <br clear="none">similarly. Instead of using < we can use the built-in function <br clear="none">cmp(a, b), which returns -1 if a < b, 0 if a == b, and +1 if a > b.<br clear="none"><br clear="none">Or instead of using the built-in cmp function, we can use any function <br clear="none">that takes two arguments, the items to be compared, and returns one of <br clear="none">-1, 0 or 1.<br clear="none"><br clear="none">Even though the Python list.sort() method is a lot faster and more <br clear="none">clever than what I show above, it too
 allows you to provide a custom <br clear="none">comparison function to decide which comes earlier or later when sorting.<br clear="none">Here's an example with and without a comparison function:<br clear="none"><br clear="none">py> sorted(['dog', 'aardvark', 'chicken', 'horse'])<br clear="none">['aardvark', 'chicken', 'dog', 'horse']<br clear="none"><br clear="none">py> sorted(['dog', 'aardvark', 'chicken', 'horse'], <br clear="none">...   lambda a, b: cmp(len(a), len(b)))<br clear="none">['dog', 'horse', 'chicken', 'aardvark']<br clear="none"><br clear="none">In the second case, we sort by the length of the words, not the content <br clear="none">of the word. So "dog" (three letters) compares less than "aardvark" <br clear="none">(eight letters). A couple of other notes:<br clear="none"><br clear="none">- Rather than define a comparison function using def, I use lambda as <br clear="none">  a shortcut. lambda creates a function, but
 limited only to a single<br clear="none">  expression. So "lambda a, b: cmp(len(a), len(b))" is equivalent to:<br clear="none"><br clear="none">  def function(a, b):<br clear="none">      return cmp(len(a), len(b))<br clear="none"><br clear="none">- Notice that I use the built-in cmp function inside my comparison <br clear="none">  function. That's just for convenience, you don't have to do that.<br clear="none"><br clear="none"><br clear="none">> How obsolete are the cmp() and the decorate-sort-undecorate methods?  <br clear="none">> To be understood but probably not used in new code?<br clear="none"><br clear="none">Both are very obsolute, but for different reasons.<br clear="none"><br clear="none">The problem with using a comparison function is that it is very <br clear="none">inefficient and it can really slow down sorting of large lists by a lot. <br clear="none">It is better to use the DSU idiom rather than
 call a comparison <br clear="none">function. In fact, that is so much better, that recent versions of <br clear="none">Python make the DSU idiom built-in: that's what the "key" argument to <br clear="none">the sort() and sorted() functions is for. When you supply a key function <br clear="none">to sort, it internally uses the DSU idiom. You almost never need to use <br clear="none">it yourself.<br clear="none"><br clear="none">Using the key function is so much better than using a comparison <br clear="none">function that in Python 3 the comparison function was dropped <br clear="none">altogether and using key is the only way to customize sorting.<div class="yqt1757505052" id="yqtfd46231"><br clear="none"><br clear="none"><br clear="none">> Python Programming, Zelle;  Python 2.7.3,  PowerShell, Notepad ++<br clear="none">> <br clear="none">> I tried several means of sorting for exercises, eg</div><br clear="none">[lots of code
 shown]<br clear="none"><br clear="none"><br clear="none">I'm sorry, did you have a question about the sorting code or were you <br clear="none">just sharing it with us? If you're asking which should be preferred, I <br clear="none">would prefer the version using the key=... argument to sort.<br clear="none"><br clear="none"><br clear="none"><br clear="none">-- <br clear="none">Steven<div class="yqt1757505052" id="yqtfd21594"><br clear="none"></div><br><br></div> </div> </div>  </div></body></html>