l1=[3,1,4,6,7]
l2=sorted(l1)
print(l1,l2) #l2 is a different list from l1
l1.sort() #now l1 is sorted in-place
print(l1)

"""
Explanation: Some remarks on sorting
You can either use the sorted() Python built-in function which will produce a sorted version of anything you can iterate over. Lists have a .sort() method which sorts them in-place.
End of explanation
"""


print(sorted(l1,reverse=True))

"""
Explanation: Sort in reverse order:
reverse=True
End of explanation
"""


def lenk(s):
    """This function simply returns the length of s"""
    return len(s)

l=["sjsjsj","zzz","aaaaaaaaaaaaaaaaaa"]
print("Default sort order:", sorted(l))
print("New sort order by length:", sorted(l,key=lenk)) #Note: not lenk(), just lenk
print("New sort order by length (lambda):", sorted(l,key=lambda s: len(s))) #Same but with lambda functions

"""
Explanation: Define the sort order
You can define a function which extracts the key by which to sort from the elements of the array you are sorting. For example, if you want to sort strings by their length, you would write a function which gives the length of the string and tell sort() to use it. Like so:
End of explanation
"""


s="ACGTTGGCCAGATCGACTATGGCAGATTGACTAGCATACGATCGCATCAGAT"
counter={}
for char in s:
    counter[char]=counter.get(char,0)+1 #Easy way to count items into a dictionary
#Now we want to print the dictionary sorted by count from most to least common and 
#in case two letters have the same count, we want them sorted alphabetically

def get_val(key_value):
    """Gets one key-value pair from the dictionary and returns the value, and then the key"""
    return key_value[1], key_value[0] #the value is the first thing we sort on, the letter the second

items=list(counter.items()) #all items from the dictionary as a list of (key,value) pairs
print("Items:", items)
items.sort(reverse=True, key=get_val)
for letter,count in items:
    print(letter, count)


"""
Explanation: This is very useful for example when sorting dictionaries by their value:
End of explanation
"""


