길이우선정렬 구현


1 PHP[ | ]

$fruits = ['banana','apple22','apple1','apple2','apple1','apple','lemon'];
usort($fruits, function($a,$b) {
    $lendiff = strlen($a) - strlen($b);
    return ($lendiff == 0) ? strcmp($a,$b) : $lendiff;
});
print_r($fruits);
$fruits = ['바나나','사과22','사과1','사과2','사과1','사과','레몬'];
usort($fruits, function($a,$b) {
    $lendiff = strlen($a) - strlen($b);
    return ($lendiff == 0) ? strcmp($a,$b) : $lendiff;
});
print_r($fruits);

2 Python[ | ]

fruits = ['banana','apple22','apple1','apple2','apple1','apple','lemon']
fruits.sort(key=lambda s: (len(s),s))
print( fruits )
fruits = ['바나나','사과22','사과1','사과2','사과1','사과','레몬']
fruits.sort(key=lambda s: (len(s),s))
print( fruits )

3 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}