36 lines
902 B
Python
36 lines
902 B
Python
#!/usr/bin/env python
|
|
|
|
fruit = ["pomegranate", "cherry", "apricot", "date", "Apple", "lemon",
|
|
"Kiwi", "ORANGE", "lime", "Watermelon", "guava", "papaya", "FIG",
|
|
"pear", "banana", "Tamarind", "persimmon", "elderberry", "peach",
|
|
"BLUEberry", "lychee", "grape"]
|
|
|
|
def ignore_case(item): # <1>
|
|
return item.lower() # <2>
|
|
|
|
fs1 = sorted(fruit, key=ignore_case) # <3>
|
|
print("Ignoring case:")
|
|
print(" ".join(fs1), end="\n\n")
|
|
|
|
def by_length_then_name(item):
|
|
return (len(item), item.lower()) # <4>
|
|
|
|
fs2 = sorted(fruit, key=by_length_then_name)
|
|
print("By length, then name:")
|
|
print(" ".join(fs2))
|
|
print()
|
|
|
|
nums = [800, 80, 1000, 32, 255, 400, 5, 5000]
|
|
|
|
n1 = sorted(nums) # <5>
|
|
print("Numbers sorted numerically:")
|
|
for n in n1:
|
|
print(n, end=' ')
|
|
print("\n")
|
|
|
|
n2 = sorted(nums, key=str) # <6>
|
|
print("Numbers sorted as strings:")
|
|
for n in n2:
|
|
print(n, end=' ')
|
|
print()
|