Open In App

Python | Sort words of sentence in ascending order

Last Updated : 17 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a sentence, sort it alphabetically in ascending order. 

Examples:

Input : to learn programming refer geeksforgeeks
Output : geeksforgeeks learn programming refer to

Input : geeks for geeks
Output : for geeks geeks

Approach 1 : We will use the built-in library function to sort the words of the sentence in ascending order. 

Prerequisites: 

Follow the steps below:

  • Split the Sentence in words.
  • Sort the words alphabetically
  • Join the sorted words alphabetically to form a new Sentence.

Below is the implementation of the above idea.

Python3




# Function to sort the words
# in ascending order
 
 
def sortedSentence(Sentence):
 
    # Splitting the Sentence into words
    words = Sentence.split(" ")
 
    # Sorting the words
    words.sort()
 
    # Making new Sentence by
    # joining the sorted words
    newSentence = " ".join(words)
 
    # Return newSentence
    return newSentence
 
# Driver's Code
 
 
Sentence = "to learn programming refer geeksforgeeks"
# Print the sortedSentence
print(sortedSentence(Sentence))
 
Sentence = "geeks for geeks"
# Print the sortedSentence
print(sortedSentence(Sentence))


Output

geeksforgeeks learn programming refer to
for geeks geeks

Time Complexity : O(nlogn) , due to built-in sort function used.
Auxiliary Space : O(n) , where n is the total number of characters in string. 

Approach – 2 : 

In this approach we will use the built-in split() method alongside the sorted() function to achieve the same in less lines and less use of functions and methods. We will not use the join() method here.

Python3




def sort_alphabetically(sentence : str) -> list():
   
  s = sentence.split(" ")
   
  return sorted(s)
 
 
sen = "to learn programming refer geeksforgeeks"
print(sort_alphabetically(sen))
 
sen = "geeks for geeks"
print(sort_alphabetically(sen))


Output

['geeksforgeeks', 'learn', 'programming', 'refer', 'to']
['for', 'geeks', 'geeks']

Time Complexity – O(nlogn) # Due to the use of sorted() built in function.
Space Complexity – O(n) # ‘n’ number of characters in string



Similar Reads

Sort all even numbers in ascending order and then sort all odd numbers in descending order
Given an array of integers (both odd and even), sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.Examples: Input: arr[] = {1, 2, 3, 5, 4, 7, 10}Output: arr[] = {7, 5, 3, 1, 2, 4, 10} Input: arr[] = {0, 4, 5, 3, 7, 2, 1}Output: arr[]
15+ min read
Sort first k values in ascending order and remaining n-k values in descending order
Given an array of size n, arrange the first k elements of the array in ascending order and the remaining n-k elements in descending order. Examples: Input: arr[] = {5, 4, 6, 2, 1, 3, 8, 9, -1}, k = 4 Output: 2 4 5 6 9 8 3 1 -1 Input: arr[] = {5, 4, 6}, k = 2 Output: 4 5 6 Algorithm: Store the first k elements in an array and sort that in ascending
11 min read
Sort an array of strings in ascending order with each string sorted in descending order
Given a array of strings S[] of size N (1 ? N ? 105), sort characters of each string in descending order and then print the array of strings in ascending order. Examples: Input: s[] = {"apple", "box", "cat"} Output: pplea tca xob Explanation: Sorting each string in descending order, S[] modifies to {"pplea", "xob", "tca"}. Sorting the array in asce
9 min read
Python program to sort digits of a number in ascending order
Given an integer N, the task is to sort the digits in ascending order. Print the new number obtained after excluding leading zeroes. Examples: Input: N = 193202042Output: 1222349Explanation: Sorting all digits of the given number generates 001222349.Final number obtained after removal of leading 0s is 1222349. Input: N = 78291342023Output:122233478
2 min read
Python program to sort Palindrome Words in a Sentence
Given a string S representing a sentence, the task is to reorder all the palindromic words present in the sentence in sorted order. Examples: Input: S = "Please refer to the madam to know the level"Output: Please level to the madam to know the referExplanation: Here "refer", "madam", "level" are the palindromic words. Sorting them generates the seq
5 min read
Sorting rows of matrix in ascending order followed by columns in descending order
Given a matrix, sort the rows of matrix in ascending order followed by sorting the columns in descending order. Examples : Input : a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output : 7 8 9 4 5 6 1 2 3 Input : a[3][3] = {{3, 2, 1}, {9, 8, 7}, {6, 5, 4}}; Output : 7 8 9 4 5 6 1 2 3 Approach: Traverse all rows one by one and sort rows in ascending o
10 min read
Sorting rows of matrix in descending order followed by columns in ascending order
Given a matrix of distinct elements. The task is to sort the rows of matrix in descending order followed by sorting the columns in ascending order. Examples: Input: a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output: 3 2 1 6 5 4 9 8 7 Input: a[3][3] = {{3, 2, 1}, {9, 8, 7}, {6, 5, 4}}; Output: 3 2 1 6 5 4 9 8 7 Approach: Traverse all rows one by o
9 min read
Modify a sentence by reversing order of occurrences of all Palindrome Words
Given a string S representing a sentence, the task is to reverse the order of all palindromic words present in the sentence. Examples: Input: S = "mom and dad went to eye hospital"Output: eye and dad went to mom hospitalExplanation: All palindromic words present in the string are "mom", "dad" and "eye", in the order of their occurrence. Reversing t
7 min read
Sort an array of string of dates in ascending order
Given an array of strings dates[], the task is to sort these dates in ascending order. Note: Each date is of the form dd mmm yyyy where: Domain of dd is [0-31].Domain of mmm is [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec].And, yyyy is a four digit integer. Examples: Input: dates[] = {"01 Mar 2015", "11 Apr 1996", "22 Oct 2007"} Outp
7 min read
C program to sort an array in ascending order
Sorting an array in ascending order means arranging the elements from smallest element to largest element. In this article, we will learn to sort an array in ascending order in the C programming language. There are many ways by which the array can be sorted in ascending order, For simplicity we will be using Selection Sort in this article. Algorith
2 min read
Article Tags :
Practice Tags :