Question

How to sort out string variable

  • 17 November 2023
  • 1 reply
  • 66 views

Badge +4

I want to create a small bot to sort out string variables that contain numbers in it. Please, can you point out how to make this bot work? 

Thanks


1 reply

Userlevel 3
Badge +7

Hi @jsegura 2409 ,

You can use below python code and use it into python commands. Function will be “sort_numbers_in_string” input you can pass the string variable which needs to be sorted and output need to be a string variable which gives out the output.

Remember this would require python installation on the machine where the bot will be running.

import re

def sort_numbers_in_string(input_string):
    # Find all numbers in the input string using regular expression
    numbers_list = re.findall(r'\d+', input_string)

    # Convert the matched numbers from strings to integers and sort them
    sorted_numbers = sorted(map(int, numbers_list))

    return sorted_numbers

Reply