Skip to content

Instantly share code, notes, and snippets.

@tanish-kr
Last active August 21, 2018 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanish-kr/696b1176e7da3ea6bad3 to your computer and use it in GitHub Desktop.
Save tanish-kr/696b1176e7da3ea6bad3 to your computer and use it in GitHub Desktop.
【メモ】Python3 list sort ref: https://qiita.com/kitaro_tn/items/7dcd85e610b027b54478
list = [1,3,2,4]
list.sort()
print(list)
# [1, 2, 3, 4]
list = [1,3,2,4]
print(sorted(list))
# [1, 2, 3, 4]
list = [1,3,2,4]
list.sort(reverse=True)
print(list)
# [4, 3, 2, 1]
dict = {"a","c","d","b"}
print(sorted(dict))
# ['a', 'b', 'c', 'd']
list_dict = [{"no":1,"name":"Devit"},{"no":4,"name":"Josh"},{"no":2,"name":"Sam"},{"no":3,"name":"Tom"}]
print(sorted(list_dict,key=lambda x:x["no"]))
# [{'name': 'Devit', 'no': 1}, {'name': 'Sam', 'no': 2}, {'name': 'Tom', 'no': 3}, {'name': 'Josh', 'no': 4}]
# 降順
print(sorted(list_dict,key=lambda x:x["no"],reverse=True))
# [{'name': 'Josh', 'no': 4}, {'name': 'Tom', 'no': 3}, {'name': 'Sam', 'no': 2}, {'name': 'Devit', 'no': 1}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment