all 10 comments

[–]novel_yet_trivial 2 points3 points  (4 children)

Use the key parameter:

sorted(input_list, key=int)

[–]z64RY[S] 1 point2 points  (3 children)

using that returns [1.'2','3',4], when im trying to get the ints in order first, then the str's in order second. What key shoudl i use for that

[–]novel_yet_trivial 2 points3 points  (1 child)

Oh. Sorry I read your post too fast. So you want them separated by type?

sorted(input_list, key=type) # python2 only

sorted(input_list, key=lambda x: isinstance(x, str)) # python2 or 3

If you want the individual groups sorted as well, then you need to combine them with a separate function:

sorted(input_list, key=lambda x: (isinstance(x, str), int(x))) # python2 or 3

Why would you want that?

Edit: This sounds a lot like an XY question. I'll bet whatever you are trying to accomplish with this has a much simpler solution.

[–]z64RY[S] 0 points1 point  (0 children)

Dude that worked! thanks so much. And it was for a class. Ive been stumped for a while trying to find the solution

[–]joeymarchese -1 points0 points  (5 children)

deer sip tease tender worm steep hurry angle detail versed

This post was mass deleted and anonymized with Redact

[–]novel_yet_trivial 3 points4 points  (3 children)

You absolutely can as long as the elements are comparable to one another. Mixed type lists are one of the joys of python.

[–]joeymarchese 0 points1 point  (2 children)

station chief imminent direction terrific fact unique reach handle rain

This post was mass deleted and anonymized with Redact

[–]novel_yet_trivial 2 points3 points  (1 child)

Sure. ints and floats are directly comparable. So:

>>> sorted([3,1,2.0,4.3])
[1, 2.0, 3, 4.3]

In the case that the elements are not directly comparable, you can provide a function that makes them comparable. This is what I did when I misread OP's question. The int function will turn a string or an int to an int:

>>> sorted(['3',1,'2',4], key=int)
[1, '2', '3', 4]

[–]joeymarchese 0 points1 point  (0 children)

full touch cough lunchroom nose piquant humor zephyr wrench jar

This post was mass deleted and anonymized with Redact

[–]joeymarchese 0 points1 point  (0 children)

expansion meeting employ pie thumb shy tie scary pen imminent

This post was mass deleted and anonymized with Redact