[Python] 문자열 정렬

Melon Coder·2023년 4월 19일

Python

목록 보기
4/4
post-thumbnail

정렬 알고리즘 문제를 풀다가 문자열로 이루어진 배열을 정렬하는 문제가 있었는데, sort() 메서드가 문자열도 정렬해주는지 몰랐었다.

sort()

arr = ['banana', 'apple', 'orange']
arr.sort()

>>> arr = ['apple', 'banana', 'orange']

sort(key=len)

arr = ['abcd', 'ab', 'abc']
arr.sort(key=len)

>>> arr = ['ab', 'abc', 'abcd']
profile
create new things

0개의 댓글