How does min() sort a list like this: ["abc", "abb", "aba"]
, in the end I know the output of min(["abc", "abb", "aba"])
will be "aba"
but I'm not quite sure how it decides on which one to consider the min, does it sort by the string with the lowest sum of ord() for each character in the string? Or how does it sort it?
Asked
Active
Viewed 59 times
-1

Juan Garcia
- 15
- 4
-
1What makes you think `min` sorts the list? – ggorlen Apr 26 '20 at 17:59
-
4It compares the strings *lexicographically*. – jonrsharpe Apr 26 '20 at 18:00
-
@ggorlen I meant returns the value of the min() character, I just wasnt focused properly – Juan Garcia Apr 26 '20 at 18:03
-
It might be taking ASCII value of that like a=97,b=98,c=99 abc means 979899 abb means 979898 and aba means 979897 which is small from both of them. – ROHAN TONGLE Apr 26 '20 at 18:05
1 Answers
0
It loops through each and every value and picks up the smallest one. Equivalent code:
def min(lst):
me = lst[0]
for i in range(1, len(lst)):
if lst[i] < me:
me = lst[i]
return me
It's a naive implementation but it gives you an idea.

Tarik
- 10,810
- 2
- 26
- 40