#!/usr/bin/env python3
###################################################################################
# Python sort() method - with custom sorts
#        sort() can only sort a list  
#        sort() sorts in place
###################################################################################
print("Content-Type: text/html \n")     #http header with newline char (required for the web)
print('<pre>')                                      #enhance display for the web

#-- define sort functions ----------------------------------------------------

def by_char2(element):                           #function to sort starting from char 2
    char2 = element[1:]                          #return the portion to sort on
    return char2

def by_lastname(element):                        #function to sort by lastname
    lastname = element[element.index(' ')+1 : ]  #find space, take all the way to end   
    return lastname

def by_lastname_firstname(element):              #function to sort by lastname
    firstname = element[:element.index(' ')]     #find space, take from start to space   
    lastname  = element[element.index(' ')+1:]   #find space, take all the way to end
    sort_keys = (lastname, firstname)            #sort keys is a tuple of (lastname, firstname)   
    return sort_keys

def by_2dim_lastname(element):                   #function to sort 2 dimensional list
    lastname  = element[1]                       #take element[1] which is lastname
    return lastname

def by_2dim_lastname_firstname(element):         #function to sort 2 dimensional list
    firstname = element[0]                       #take element[1] which is lastname
    lastname  = element[1]                       #take element[1] which is lastname
    return (lastname, firstname)


print("\n--------------1 dimensional list ------------------------------------------------------------")

names  = ['Sam Sultan', 'George Washington', 'Michael Sultan', 'Abraham Lincoln', 'Donald Duck']
print("The original list.: ", names)
print()

names.sort()
print("Sort ascending....: ", names)               #sort ascending
print()

names.sort(reverse=True)                           #sort descending
print("Sort descending...: ", names)
print()

names.sort(key=by_char2)                           #sort by 2nd character
print("Sort by 2nd char..: ", names)  
print()

names.sort(key=by_lastname)                        #sort by lastname
print("Sort by lastname..: ", names)  
print()

names.sort(key=by_lastname_firstname)              #sort by lastname then firstname
print("Sort by lastname & firstname: ", names)  
print()


print("\n--------------2 dimensional list ------------------------------------------------------------")

dim2 = [['Sam','Sultan'],['George','Washington'],['Michael','Sultan'],['Abraham','Lincoln'],['Donald','Duck']] 
print("The original 2 dimensional list: ") 
print("[", end="")
for row in dim2:
    print('\t' , row)
print("]", '\n')

dim2.sort()                                       #sort 2 dim ascending
print("Sort ascending....: ")
print("[", end="")
for row in dim2:
    print('\t' , row)
print("]", '\n')

dim2.sort(reverse=True)                           #sort 2 dim descending
print("Sort descending...: ")
print("[", end="")
for row in dim2:
    print('\t' , row)
print("]", '\n')

dim2.sort(key=by_2dim_lastname)                   #sort 2 dim by custom function
print("Sort by lastname..: ")
print("[", end="")
for row in dim2:
    print('\t' , row)
print("]", '\n')

dim2.sort(key=by_2dim_lastname_firstname)         #sort 2 dim by custom function
print("Sort by lastname then firstname:")
print("[", end="")
for row in dim2:
    print('\t' , row)
print("]", '\n')

dim2.sort(key=lambda elem: elem[1])               #sort 2 dim using a lambda function
print("Sort by lastname (using lambda function):")
print("[", end="")
for row in dim2:
    print('\t' , row)
print("]", '\n')