How to sort words in a sentence in ascending order? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to sort words in a sentence in ascending order?

At the input we get the line "This is the first time I've added a sentence in Dutch", at the output we get "a in is the I've This time first Dutch added sentence" https://code.sololearn.com/ciXI8i9uRc0o/?ref=app

21st Jan 2021, 7:16 PM
Mary_d9
Mary_d9 - avatar
6 Answers
+ 7
This should help: items = [x for x in (input() or "this is sorting test").split(" ")] print(items) print(sorted(items, key = len))
21st Jan 2021, 9:07 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
What is the sorting key? length of word? or something else?
21st Jan 2021, 7:26 PM
Ipang
+ 3
Mary_d9 please clarify your question as it appears abit confusing short of saying ``` x = sorted(["Tom", "Jerry", "Felix", "Stewart Liitle"]) print(x) ``` output [ Felix , Jerry, Stewart Little, Tom ]
21st Jan 2021, 8:25 PM
BroFar
BroFar - avatar
+ 2
Ipang length of word
21st Jan 2021, 7:44 PM
Mary_d9
Mary_d9 - avatar
+ 2
Not clear to me what you are to do, can you provide an example of input and expected output?
21st Jan 2021, 8:15 PM
Abhay
Abhay - avatar
+ 1
Mary_d9 If you only want to sort the elements by their length then look at Rik Wittkopp's recommendation 👍 But it is still unclear what you meant by "list with numbers can somehow be connected with list of string". Please elaborate more on that part by adding bit more detail in the Description. P.S. Are you referring to this newly approved challenge thread? https://www.sololearn.com/Discuss/2667888/?ref=app
22nd Jan 2021, 12:04 AM
Ipang