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'









share|improve this question



















  • 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














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'









share|improve this question



















  • 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












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'









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












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.






share|improve this answer






















  • "[(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 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










  • @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

















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']







share|improve this answer






















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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.






share|improve this answer






















  • "[(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 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










  • @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














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.






share|improve this answer






















  • "[(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 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










  • @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












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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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










  • @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
















  • "[(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 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










  • @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















"[(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












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']







share|improve this answer






















  • 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














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']







share|improve this answer






















  • 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












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']







share|improve this answer














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']








share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線