Given an** integer X** and a string **str** consisting of space-separated **texts** and **numbers** placed alternately, the task is to sort the string such that texts and numbers appear in decreasing order of numbers associated after removal of all numbers less than **X**. If two strings have the same values then sort them in the lexicographic order of the string.

**Examples:**

Input:X = 79, str = “Geek 78 for 99 Geeks 88 Gfg 86”

Output:

for 99 Geeks 88 Gfg 86

Explanation:

“Eve 99” is removed, since the number associated to it is smaller than X(= 79)

Now, the reordered string string based on decreasing order of numbers associated is “Bob 99 Suzy 88 Alice 86”.

Input:X = 77, str = “Axc 78 Czt 60”

Output:Axc 78

**Approach: **The idea is to use the Bubble Sort technique. Follow the steps below to solve the problem:

- Split the string in to a
**list**, then remove the entries that are less than the given value i.e.**X**. - Sort the list based on the number associated with it using bubble sort method.
- If the numbers are not equal, sort the numbers in decreasing order and simultaneously sort the names.
- If the numbers are equal then sort them lexicographically.
- Swap both the strings and the number together in order to keep them together.

Below is the implementation of the above approach:

## Python3

`# Python Program to implement ` `# the above approach ` ` ` `# Function to split the input ` `# string into a list ` `def` `tokenizer(` `Str` `): ` ` ` `List` `=` `Str` `.split() ` ` ` `return` `List` ` ` `# Function to sort the given list based ` `# on values at odd indices of the list ` `def` `SortListByOddIndices(` `List` `, x): ` ` ` ` ` `l ` `=` `len` `(` `List` `) ` ` ` `# Function to remove the values ` `# less than the given value x ` ` ` `for` `i ` `in` `range` `(l ` `-` `1` `, ` `0` `, ` `-` `2` `): ` ` ` `if` `int` `(` `List` `[i]) < x: ` ` ` `del` `(` `List` `[i ` `-` `1` `: i ` `+` `1` `]) ` ` ` ` ` `l ` `=` `len` `(` `List` `) ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, l, ` `2` `): ` ` ` `for` `j ` `in` `range` `(` `1` `, l ` `-` `i, ` `2` `): ` ` ` ` ` `# Compares values at odd indices of ` ` ` `# the list to sort the list ` ` ` `if` `List` `[j] < ` `List` `[j ` `+` `2` `] \ ` ` ` `or` `(` `List` `[j ` `-` `1` `] > ` `List` `[j ` `+` `1` `] \ ` ` ` `and` `List` `[j] ` `=` `=` `List` `[j ` `+` `2` `]): ` ` ` ` ` `List` `[j], ` `List` `[j ` `+` `2` `] \ ` ` ` `=` `List` `[j ` `+` `2` `], ` `List` `[j] ` ` ` ` ` `List` `[j ` `-` `1` `], ` `List` `[j ` `+` `1` `] \ ` ` ` `=` `List` `[j ` `+` `1` `], ` `List` `[j ` `-` `1` `] ` ` ` ` ` `return` `' '` `.join(` `List` `) ` ` ` ` ` `# Driver Code ` `Str` `=` `"Axc 78 Czy 60"` `x ` `=` `77` ` ` `# Function call ` `List` `=` `tokenizer(` `Str` `) ` ` ` `Str` `=` `SortListByOddIndices(` `List` `, x) ` ` ` `print` `(` `Str` `) ` |

*chevron_right*

*filter_none*

**Output:**

Axc 78

**Time Complexity: **O(N^{2})

**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sort first k values in ascending order and remaining n-k values in descending order
- Delete array elements which are smaller than next or become smaller
- Maximum removal from array when removal time >= waiting time
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Find the minimum value from an array associated with another array
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Heap Sort for decreasing order using min heap
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Minimum sum possible by assigning every increasing/decreasing consecutive pair with values in that order
- Find the element before which all the elements are smaller than it, and after which all are greater
- Maximum length of balanced string after swapping and removal of characters
- Lexicographically largest possible String after removal of K characters
- Longest palindromic string possible after removal of a substring
- Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1'
- Maximize a number considering permutations with values smaller than limit
- Generate a Number in Decreasing order of Frequencies of characters of a given String
- Ropes left after every removal of smallest
- Minimize the maximum minimum difference after one removal from array
- Count of strings that become equal to one of the two strings after one removal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.