Sorting a mixed list of ints and strings
up vote
-3
down vote
favorite
I am trying to sort the following mixed list of ints and strings, but getting a TypeError instead. My desired output order is sorted integers then sorted strings.
x=[4,6,9,'ashley','drooks','chay','poo','may']
>>> x.sort()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
x.sort()
TypeError: '<' not supported between instances of 'str' and 'int'
python python-3.x list sorting mixed
|
show 2 more comments
up vote
-3
down vote
favorite
I am trying to sort the following mixed list of ints and strings, but getting a TypeError instead. My desired output order is sorted integers then sorted strings.
x=[4,6,9,'ashley','drooks','chay','poo','may']
>>> x.sort()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
x.sort()
TypeError: '<' not supported between instances of 'str' and 'int'
python python-3.x list sorting mixed
4
And how are they supposed to sort exactly?
– Jon Clements♦
Apr 14 at 9:05
3
What does it mean to sort numbers and strings together?
– Oliver Charlesworth
Apr 14 at 9:05
4
It's telling you exactly what the problem is. But you, on the other hand, haven't told us what the sorted list would look like. Would numbers be sorted before strings? Or after strings? We can't fix your code without knowing what you want it to do.
– Aran-Fey
Apr 14 at 9:05
Ideally what should happen? I thought of sorting list items here. I am not sure how python will handle this? I thought integers will be sorted first and strings in the end.
– Aayush
Apr 14 at 9:07
Ideally you get told it doesn't make any natural sense to sort such things together (which you have). You then decide what the rules are depending on what output you want... :)
– Jon Clements♦
Apr 14 at 9:08
|
show 2 more comments
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I am trying to sort the following mixed list of ints and strings, but getting a TypeError instead. My desired output order is sorted integers then sorted strings.
x=[4,6,9,'ashley','drooks','chay','poo','may']
>>> x.sort()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
x.sort()
TypeError: '<' not supported between instances of 'str' and 'int'
python python-3.x list sorting mixed
I am trying to sort the following mixed list of ints and strings, but getting a TypeError instead. My desired output order is sorted integers then sorted strings.
x=[4,6,9,'ashley','drooks','chay','poo','may']
>>> x.sort()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
x.sort()
TypeError: '<' not supported between instances of 'str' and 'int'
python python-3.x list sorting mixed
python python-3.x list sorting mixed
edited Nov 11 at 6:26
smci
14.4k672104
14.4k672104
asked Apr 14 at 9:04
Aayush
12
12
4
And how are they supposed to sort exactly?
– Jon Clements♦
Apr 14 at 9:05
3
What does it mean to sort numbers and strings together?
– Oliver Charlesworth
Apr 14 at 9:05
4
It's telling you exactly what the problem is. But you, on the other hand, haven't told us what the sorted list would look like. Would numbers be sorted before strings? Or after strings? We can't fix your code without knowing what you want it to do.
– Aran-Fey
Apr 14 at 9:05
Ideally what should happen? I thought of sorting list items here. I am not sure how python will handle this? I thought integers will be sorted first and strings in the end.
– Aayush
Apr 14 at 9:07
Ideally you get told it doesn't make any natural sense to sort such things together (which you have). You then decide what the rules are depending on what output you want... :)
– Jon Clements♦
Apr 14 at 9:08
|
show 2 more comments
4
And how are they supposed to sort exactly?
– Jon Clements♦
Apr 14 at 9:05
3
What does it mean to sort numbers and strings together?
– Oliver Charlesworth
Apr 14 at 9:05
4
It's telling you exactly what the problem is. But you, on the other hand, haven't told us what the sorted list would look like. Would numbers be sorted before strings? Or after strings? We can't fix your code without knowing what you want it to do.
– Aran-Fey
Apr 14 at 9:05
Ideally what should happen? I thought of sorting list items here. I am not sure how python will handle this? I thought integers will be sorted first and strings in the end.
– Aayush
Apr 14 at 9:07
Ideally you get told it doesn't make any natural sense to sort such things together (which you have). You then decide what the rules are depending on what output you want... :)
– Jon Clements♦
Apr 14 at 9:08
4
4
And how are they supposed to sort exactly?
– Jon Clements♦
Apr 14 at 9:05
And how are they supposed to sort exactly?
– Jon Clements♦
Apr 14 at 9:05
3
3
What does it mean to sort numbers and strings together?
– Oliver Charlesworth
Apr 14 at 9:05
What does it mean to sort numbers and strings together?
– Oliver Charlesworth
Apr 14 at 9:05
4
4
It's telling you exactly what the problem is. But you, on the other hand, haven't told us what the sorted list would look like. Would numbers be sorted before strings? Or after strings? We can't fix your code without knowing what you want it to do.
– Aran-Fey
Apr 14 at 9:05
It's telling you exactly what the problem is. But you, on the other hand, haven't told us what the sorted list would look like. Would numbers be sorted before strings? Or after strings? We can't fix your code without knowing what you want it to do.
– Aran-Fey
Apr 14 at 9:05
Ideally what should happen? I thought of sorting list items here. I am not sure how python will handle this? I thought integers will be sorted first and strings in the end.
– Aayush
Apr 14 at 9:07
Ideally what should happen? I thought of sorting list items here. I am not sure how python will handle this? I thought integers will be sorted first and strings in the end.
– Aayush
Apr 14 at 9:07
Ideally you get told it doesn't make any natural sense to sort such things together (which you have). You then decide what the rules are depending on what output you want... :)
– Jon Clements♦
Apr 14 at 9:08
Ideally you get told it doesn't make any natural sense to sort such things together (which you have). You then decide what the rules are depending on what output you want... :)
– Jon Clements♦
Apr 14 at 9:08
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
6
down vote
You can pass a custom key function to list.sort
:
x = [4,6,9,'ashley','drooks','chay','poo','may']
x.sort(key=lambda v: (isinstance(v, str), v))
# result:
# [4, 6, 9, 'ashley', 'chay', 'drooks', 'may', 'poo']
This key function maps each element in the list to a tuple in which the first value is a boolean (True
for strings and False
for numbers) and the second value is the element itself, like this:
>>> [(isinstance(v, str), v) for v in x]
[(False, 4), (False, 6), (False, 9), (True, 'ashley'), (True, 'chay'),
(True, 'drooks'), (True, 'may'), (True, 'poo')]
These tuples are then used to sort the list. Because False < True
, this makes it so that integers are sorted before strings. Elements with the same boolean value are then sorted by the 2nd value in the tuple.
"[(isinstance(v, str), v) for v in x]" I have never seen this statement. Can you please elaborate this with an example. I am not able to visualize what this statement do?
– Aayush
Apr 14 at 10:00
x.sort(key=lambda v: (isinstance(v, str), v)). What does key lambda means and why we use it? What does isinstance is used for? "isinstance(v, str), v)"- what operation this line performs?
– Aayush
Apr 14 at 10:03
1
@Aayush That's too much to explain here. Take a look at list comprehensions and lambda functions. The code(isinstance(v, str), v)
creates a tuple where the first element is a boolean (the result ofisinstance(v, str)
) and the second element isv
.
– Aran-Fey
Apr 14 at 10:07
@Aran-Fey I understand key=lambda v: (isinstance(v, str), v) returns (False,4) and so on i still don't get is how value v is passed to lambda
– amitnair92
Apr 14 at 10:40
@amitnair92 The lambda is called bylist.sort
. It passes each value in the list to the key function as an argument, andv
takes the value of that argument.
– Aran-Fey
Apr 14 at 11:00
|
show 1 more comment
up vote
1
down vote
I can see from your comment that you want integers to be sorted first then strings.
So we could sort two separate lists and join them as follows:
x=[4,6,9,'ashley','drooks','chay','poo','may']
intList=sorted([i for i in x if type(i) is int])
strList=sorted([i for i in x if type(i) is str])
print(intList+strList)
Output:
[4, 6, 9, 'ashley', 'chay', 'drooks', 'may', 'poo']
can't we do without dividing it?
– Aayush
Apr 14 at 9:13
I don't think so because you cannot compare integers with strings.
– Dan
Apr 14 at 9:14
i mean to say just sort the integer part in itself without touching the strings. Then sort the strings part. And according to ur code if i want strings first, i'll have to do like this? - print(strList+intList)
– Aayush
Apr 14 at 9:17
3
@Dan you can... you just need to make a key that has a consistently sortable field... eg:sorted(data, key=lambda L: (isinstance(L, str), L))
to put non-str's first which'll work as long as they remain orderable among themselves...
– Jon Clements♦
Apr 14 at 9:18
Oh I didn't know that.
– Dan
Apr 14 at 9:20
|
show 4 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
You can pass a custom key function to list.sort
:
x = [4,6,9,'ashley','drooks','chay','poo','may']
x.sort(key=lambda v: (isinstance(v, str), v))
# result:
# [4, 6, 9, 'ashley', 'chay', 'drooks', 'may', 'poo']
This key function maps each element in the list to a tuple in which the first value is a boolean (True
for strings and False
for numbers) and the second value is the element itself, like this:
>>> [(isinstance(v, str), v) for v in x]
[(False, 4), (False, 6), (False, 9), (True, 'ashley'), (True, 'chay'),
(True, 'drooks'), (True, 'may'), (True, 'poo')]
These tuples are then used to sort the list. Because False < True
, this makes it so that integers are sorted before strings. Elements with the same boolean value are then sorted by the 2nd value in the tuple.
"[(isinstance(v, str), v) for v in x]" I have never seen this statement. Can you please elaborate this with an example. I am not able to visualize what this statement do?
– Aayush
Apr 14 at 10:00
x.sort(key=lambda v: (isinstance(v, str), v)). What does key lambda means and why we use it? What does isinstance is used for? "isinstance(v, str), v)"- what operation this line performs?
– Aayush
Apr 14 at 10:03
1
@Aayush That's too much to explain here. Take a look at list comprehensions and lambda functions. The code(isinstance(v, str), v)
creates a tuple where the first element is a boolean (the result ofisinstance(v, str)
) and the second element isv
.
– Aran-Fey
Apr 14 at 10:07
@Aran-Fey I understand key=lambda v: (isinstance(v, str), v) returns (False,4) and so on i still don't get is how value v is passed to lambda
– amitnair92
Apr 14 at 10:40
@amitnair92 The lambda is called bylist.sort
. It passes each value in the list to the key function as an argument, andv
takes the value of that argument.
– Aran-Fey
Apr 14 at 11:00
|
show 1 more comment
up vote
6
down vote
You can pass a custom key function to list.sort
:
x = [4,6,9,'ashley','drooks','chay','poo','may']
x.sort(key=lambda v: (isinstance(v, str), v))
# result:
# [4, 6, 9, 'ashley', 'chay', 'drooks', 'may', 'poo']
This key function maps each element in the list to a tuple in which the first value is a boolean (True
for strings and False
for numbers) and the second value is the element itself, like this:
>>> [(isinstance(v, str), v) for v in x]
[(False, 4), (False, 6), (False, 9), (True, 'ashley'), (True, 'chay'),
(True, 'drooks'), (True, 'may'), (True, 'poo')]
These tuples are then used to sort the list. Because False < True
, this makes it so that integers are sorted before strings. Elements with the same boolean value are then sorted by the 2nd value in the tuple.
"[(isinstance(v, str), v) for v in x]" I have never seen this statement. Can you please elaborate this with an example. I am not able to visualize what this statement do?
– Aayush
Apr 14 at 10:00
x.sort(key=lambda v: (isinstance(v, str), v)). What does key lambda means and why we use it? What does isinstance is used for? "isinstance(v, str), v)"- what operation this line performs?
– Aayush
Apr 14 at 10:03
1
@Aayush That's too much to explain here. Take a look at list comprehensions and lambda functions. The code(isinstance(v, str), v)
creates a tuple where the first element is a boolean (the result ofisinstance(v, str)
) and the second element isv
.
– Aran-Fey
Apr 14 at 10:07
@Aran-Fey I understand key=lambda v: (isinstance(v, str), v) returns (False,4) and so on i still don't get is how value v is passed to lambda
– amitnair92
Apr 14 at 10:40
@amitnair92 The lambda is called bylist.sort
. It passes each value in the list to the key function as an argument, andv
takes the value of that argument.
– Aran-Fey
Apr 14 at 11:00
|
show 1 more comment
up vote
6
down vote
up vote
6
down vote
You can pass a custom key function to list.sort
:
x = [4,6,9,'ashley','drooks','chay','poo','may']
x.sort(key=lambda v: (isinstance(v, str), v))
# result:
# [4, 6, 9, 'ashley', 'chay', 'drooks', 'may', 'poo']
This key function maps each element in the list to a tuple in which the first value is a boolean (True
for strings and False
for numbers) and the second value is the element itself, like this:
>>> [(isinstance(v, str), v) for v in x]
[(False, 4), (False, 6), (False, 9), (True, 'ashley'), (True, 'chay'),
(True, 'drooks'), (True, 'may'), (True, 'poo')]
These tuples are then used to sort the list. Because False < True
, this makes it so that integers are sorted before strings. Elements with the same boolean value are then sorted by the 2nd value in the tuple.
You can pass a custom key function to list.sort
:
x = [4,6,9,'ashley','drooks','chay','poo','may']
x.sort(key=lambda v: (isinstance(v, str), v))
# result:
# [4, 6, 9, 'ashley', 'chay', 'drooks', 'may', 'poo']
This key function maps each element in the list to a tuple in which the first value is a boolean (True
for strings and False
for numbers) and the second value is the element itself, like this:
>>> [(isinstance(v, str), v) for v in x]
[(False, 4), (False, 6), (False, 9), (True, 'ashley'), (True, 'chay'),
(True, 'drooks'), (True, 'may'), (True, 'poo')]
These tuples are then used to sort the list. Because False < True
, this makes it so that integers are sorted before strings. Elements with the same boolean value are then sorted by the 2nd value in the tuple.
edited Apr 14 at 9:45
answered Apr 14 at 9:23
Aran-Fey
20.4k53266
20.4k53266
"[(isinstance(v, str), v) for v in x]" I have never seen this statement. Can you please elaborate this with an example. I am not able to visualize what this statement do?
– Aayush
Apr 14 at 10:00
x.sort(key=lambda v: (isinstance(v, str), v)). What does key lambda means and why we use it? What does isinstance is used for? "isinstance(v, str), v)"- what operation this line performs?
– Aayush
Apr 14 at 10:03
1
@Aayush That's too much to explain here. Take a look at list comprehensions and lambda functions. The code(isinstance(v, str), v)
creates a tuple where the first element is a boolean (the result ofisinstance(v, str)
) and the second element isv
.
– Aran-Fey
Apr 14 at 10:07
@Aran-Fey I understand key=lambda v: (isinstance(v, str), v) returns (False,4) and so on i still don't get is how value v is passed to lambda
– amitnair92
Apr 14 at 10:40
@amitnair92 The lambda is called bylist.sort
. It passes each value in the list to the key function as an argument, andv
takes the value of that argument.
– Aran-Fey
Apr 14 at 11:00
|
show 1 more comment
"[(isinstance(v, str), v) for v in x]" I have never seen this statement. Can you please elaborate this with an example. I am not able to visualize what this statement do?
– Aayush
Apr 14 at 10:00
x.sort(key=lambda v: (isinstance(v, str), v)). What does key lambda means and why we use it? What does isinstance is used for? "isinstance(v, str), v)"- what operation this line performs?
– Aayush
Apr 14 at 10:03
1
@Aayush That's too much to explain here. Take a look at list comprehensions and lambda functions. The code(isinstance(v, str), v)
creates a tuple where the first element is a boolean (the result ofisinstance(v, str)
) and the second element isv
.
– Aran-Fey
Apr 14 at 10:07
@Aran-Fey I understand key=lambda v: (isinstance(v, str), v) returns (False,4) and so on i still don't get is how value v is passed to lambda
– amitnair92
Apr 14 at 10:40
@amitnair92 The lambda is called bylist.sort
. It passes each value in the list to the key function as an argument, andv
takes the value of that argument.
– Aran-Fey
Apr 14 at 11:00
"[(isinstance(v, str), v) for v in x]" I have never seen this statement. Can you please elaborate this with an example. I am not able to visualize what this statement do?
– Aayush
Apr 14 at 10:00
"[(isinstance(v, str), v) for v in x]" I have never seen this statement. Can you please elaborate this with an example. I am not able to visualize what this statement do?
– Aayush
Apr 14 at 10:00
x.sort(key=lambda v: (isinstance(v, str), v)). What does key lambda means and why we use it? What does isinstance is used for? "isinstance(v, str), v)"- what operation this line performs?
– Aayush
Apr 14 at 10:03
x.sort(key=lambda v: (isinstance(v, str), v)). What does key lambda means and why we use it? What does isinstance is used for? "isinstance(v, str), v)"- what operation this line performs?
– Aayush
Apr 14 at 10:03
1
1
@Aayush That's too much to explain here. Take a look at list comprehensions and lambda functions. The code
(isinstance(v, str), v)
creates a tuple where the first element is a boolean (the result of isinstance(v, str)
) and the second element is v
.– Aran-Fey
Apr 14 at 10:07
@Aayush That's too much to explain here. Take a look at list comprehensions and lambda functions. The code
(isinstance(v, str), v)
creates a tuple where the first element is a boolean (the result of isinstance(v, str)
) and the second element is v
.– Aran-Fey
Apr 14 at 10:07
@Aran-Fey I understand key=lambda v: (isinstance(v, str), v) returns (False,4) and so on i still don't get is how value v is passed to lambda
– amitnair92
Apr 14 at 10:40
@Aran-Fey I understand key=lambda v: (isinstance(v, str), v) returns (False,4) and so on i still don't get is how value v is passed to lambda
– amitnair92
Apr 14 at 10:40
@amitnair92 The lambda is called by
list.sort
. It passes each value in the list to the key function as an argument, and v
takes the value of that argument.– Aran-Fey
Apr 14 at 11:00
@amitnair92 The lambda is called by
list.sort
. It passes each value in the list to the key function as an argument, and v
takes the value of that argument.– Aran-Fey
Apr 14 at 11:00
|
show 1 more comment
up vote
1
down vote
I can see from your comment that you want integers to be sorted first then strings.
So we could sort two separate lists and join them as follows:
x=[4,6,9,'ashley','drooks','chay','poo','may']
intList=sorted([i for i in x if type(i) is int])
strList=sorted([i for i in x if type(i) is str])
print(intList+strList)
Output:
[4, 6, 9, 'ashley', 'chay', 'drooks', 'may', 'poo']
can't we do without dividing it?
– Aayush
Apr 14 at 9:13
I don't think so because you cannot compare integers with strings.
– Dan
Apr 14 at 9:14
i mean to say just sort the integer part in itself without touching the strings. Then sort the strings part. And according to ur code if i want strings first, i'll have to do like this? - print(strList+intList)
– Aayush
Apr 14 at 9:17
3
@Dan you can... you just need to make a key that has a consistently sortable field... eg:sorted(data, key=lambda L: (isinstance(L, str), L))
to put non-str's first which'll work as long as they remain orderable among themselves...
– Jon Clements♦
Apr 14 at 9:18
Oh I didn't know that.
– Dan
Apr 14 at 9:20
|
show 4 more comments
up vote
1
down vote
I can see from your comment that you want integers to be sorted first then strings.
So we could sort two separate lists and join them as follows:
x=[4,6,9,'ashley','drooks','chay','poo','may']
intList=sorted([i for i in x if type(i) is int])
strList=sorted([i for i in x if type(i) is str])
print(intList+strList)
Output:
[4, 6, 9, 'ashley', 'chay', 'drooks', 'may', 'poo']
can't we do without dividing it?
– Aayush
Apr 14 at 9:13
I don't think so because you cannot compare integers with strings.
– Dan
Apr 14 at 9:14
i mean to say just sort the integer part in itself without touching the strings. Then sort the strings part. And according to ur code if i want strings first, i'll have to do like this? - print(strList+intList)
– Aayush
Apr 14 at 9:17
3
@Dan you can... you just need to make a key that has a consistently sortable field... eg:sorted(data, key=lambda L: (isinstance(L, str), L))
to put non-str's first which'll work as long as they remain orderable among themselves...
– Jon Clements♦
Apr 14 at 9:18
Oh I didn't know that.
– Dan
Apr 14 at 9:20
|
show 4 more comments
up vote
1
down vote
up vote
1
down vote
I can see from your comment that you want integers to be sorted first then strings.
So we could sort two separate lists and join them as follows:
x=[4,6,9,'ashley','drooks','chay','poo','may']
intList=sorted([i for i in x if type(i) is int])
strList=sorted([i for i in x if type(i) is str])
print(intList+strList)
Output:
[4, 6, 9, 'ashley', 'chay', 'drooks', 'may', 'poo']
I can see from your comment that you want integers to be sorted first then strings.
So we could sort two separate lists and join them as follows:
x=[4,6,9,'ashley','drooks','chay','poo','may']
intList=sorted([i for i in x if type(i) is int])
strList=sorted([i for i in x if type(i) is str])
print(intList+strList)
Output:
[4, 6, 9, 'ashley', 'chay', 'drooks', 'may', 'poo']
edited Apr 14 at 9:15
answered Apr 14 at 9:11
Dan
305311
305311
can't we do without dividing it?
– Aayush
Apr 14 at 9:13
I don't think so because you cannot compare integers with strings.
– Dan
Apr 14 at 9:14
i mean to say just sort the integer part in itself without touching the strings. Then sort the strings part. And according to ur code if i want strings first, i'll have to do like this? - print(strList+intList)
– Aayush
Apr 14 at 9:17
3
@Dan you can... you just need to make a key that has a consistently sortable field... eg:sorted(data, key=lambda L: (isinstance(L, str), L))
to put non-str's first which'll work as long as they remain orderable among themselves...
– Jon Clements♦
Apr 14 at 9:18
Oh I didn't know that.
– Dan
Apr 14 at 9:20
|
show 4 more comments
can't we do without dividing it?
– Aayush
Apr 14 at 9:13
I don't think so because you cannot compare integers with strings.
– Dan
Apr 14 at 9:14
i mean to say just sort the integer part in itself without touching the strings. Then sort the strings part. And according to ur code if i want strings first, i'll have to do like this? - print(strList+intList)
– Aayush
Apr 14 at 9:17
3
@Dan you can... you just need to make a key that has a consistently sortable field... eg:sorted(data, key=lambda L: (isinstance(L, str), L))
to put non-str's first which'll work as long as they remain orderable among themselves...
– Jon Clements♦
Apr 14 at 9:18
Oh I didn't know that.
– Dan
Apr 14 at 9:20
can't we do without dividing it?
– Aayush
Apr 14 at 9:13
can't we do without dividing it?
– Aayush
Apr 14 at 9:13
I don't think so because you cannot compare integers with strings.
– Dan
Apr 14 at 9:14
I don't think so because you cannot compare integers with strings.
– Dan
Apr 14 at 9:14
i mean to say just sort the integer part in itself without touching the strings. Then sort the strings part. And according to ur code if i want strings first, i'll have to do like this? - print(strList+intList)
– Aayush
Apr 14 at 9:17
i mean to say just sort the integer part in itself without touching the strings. Then sort the strings part. And according to ur code if i want strings first, i'll have to do like this? - print(strList+intList)
– Aayush
Apr 14 at 9:17
3
3
@Dan you can... you just need to make a key that has a consistently sortable field... eg:
sorted(data, key=lambda L: (isinstance(L, str), L))
to put non-str's first which'll work as long as they remain orderable among themselves...– Jon Clements♦
Apr 14 at 9:18
@Dan you can... you just need to make a key that has a consistently sortable field... eg:
sorted(data, key=lambda L: (isinstance(L, str), L))
to put non-str's first which'll work as long as they remain orderable among themselves...– Jon Clements♦
Apr 14 at 9:18
Oh I didn't know that.
– Dan
Apr 14 at 9:20
Oh I didn't know that.
– Dan
Apr 14 at 9:20
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49829732%2fsorting-a-mixed-list-of-ints-and-strings%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
And how are they supposed to sort exactly?
– Jon Clements♦
Apr 14 at 9:05
3
What does it mean to sort numbers and strings together?
– Oliver Charlesworth
Apr 14 at 9:05
4
It's telling you exactly what the problem is. But you, on the other hand, haven't told us what the sorted list would look like. Would numbers be sorted before strings? Or after strings? We can't fix your code without knowing what you want it to do.
– Aran-Fey
Apr 14 at 9:05
Ideally what should happen? I thought of sorting list items here. I am not sure how python will handle this? I thought integers will be sorted first and strings in the end.
– Aayush
Apr 14 at 9:07
Ideally you get told it doesn't make any natural sense to sort such things together (which you have). You then decide what the rules are depending on what output you want... :)
– Jon Clements♦
Apr 14 at 9:08