1

I have a custom sort function similar to this question. However, I have one list value which - if present - must be sorted to the end of the list (to the last location). Is this possible to implement in a custom sort function?

I had considered the following:

mylist.sort(cust_sort)

def cust_sort(item1, item2):
    if item1.key == critical_value:
        #If the item1 in question is the "must be last" item, place it last.
        return -99 
        #There should never be more than 99 items in this list, but
        #   this number could be as large as necessary.
    if item1.key > item2.key:
        return 1
    elif item1.key < item2.key:
        return -1
    if item1.name > item2.name:
        return 0

    return 0

Note: I'd like to limit the scope of my implementation of this fix as much as possible - if possible to only this custom-sort function. I'm aware that I could remove this critical value, perform the sort, and re-add the critical value afterwards. However, this is legacy code and already quite confusing - implementing the fix directly in the custom sort function keeps influences outside of the operating scope to a minimum. I'd prefer to maximize readability and minimize disturbance.

user3.1415927
  • 367
  • 3
  • 19

1 Answers1

2

Instead of creating a comparator function, create a key function:

mylist.sort(key=lambda i: (i.key == CRITICAL_VALUE, i.key, i.name))

This makes it clear (to me at least) that you first sort by i.key == CRITICAL_VALUE, then by i.key, and finally by i.name.

Blender
  • 289,723
  • 53
  • 439
  • 496
  • I'm still learning `lambda` functions. Does this redefine the .sort() function as three functions? (First sort is i.key is equal to the CRIT_VAL, second sort is regular .sort() on i.key, and third sort is .sort() on i.name? – user3.1415927 Jan 19 '18 at 01:13
  • @user3.1415927: You can do `def func(i): return (i.key == CRITICAL_VALUE, i.key, i.name)` and `mylist.sort(key=func)` if it's clearer to you, but the `lambda` really has no significance beyond creating a function. `mylist.sort(key=func)` sorts `mylist` but checks `func(a) < func(b)` instead of `a < b` when determining the order. – Blender Jan 19 '18 at 03:34