🌐
W3Schools
w3schools.com › python › ref_func_sorted.asp
Python sorted() Function
The sorted() function returns a sorted list of the specified iterable object. You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically.
🌐
Python documentation
docs.python.org › 3 › howto › sorting.html
Sorting Techniques — Python 3.14.6 documentation
Author, Andrew Dalke and Raymond Hettinger,. Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted lis...
🌐
Real Python
realpython.com › python-sort
How to Use sorted() and .sort() in Python – Real Python
February 24, 2025 - You don’t have to define the sorted() function. It’s a built-in function that’s available in any standard installation of Python. You’re ordering the values in numbers from smallest to largest when you call sorted(numbers).
🌐
GeeksforGeeks
geeksforgeeks.org › python › sort-in-python
sort() in Python - GeeksforGeeks
The sort() method in Python is used to arrange the elements of a list in a specific order. It works only on lists, modifies the original list in place, and does not return a new list.
Published   January 13, 2026
🌐
freeCodeCamp
freecodecamp.org › news › python-sort-how-to-sort-a-list-in-python
Python .sort() – How to Sort a List in Python
March 8, 2022 - As mentioned earlier, by default, sort() sorts list items in ascending order. Ascending (or increasing) order means that items are arranged from lowest to highest value. The lowest value is on the left hand side and the highest value is on the right.
🌐
Programiz
programiz.com › python-programming › methods › list › sort
Python List sort()
Python Dictionary · The list's sort() method sorts the elements of a list. prime_numbers = [11, 3, 7, 5, 2] # sort the list in ascending order prime_numbers.sort() print(prime_numbers) # Output: [2, 3, 5, 7, 11] numbers.sort(reverse, key) The sort() method can take two optional keyword arguments: ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python sort list of numbers or integers
Python Sort List of Numbers or Integers - Spark By {Examples}
May 31, 2024 - You can use the built-in sorted() function in Python to sort a list of numbers or integer values. This method will return a new list after sorting list.
🌐
Hyperskill
hyperskill.org › university › python › sorting-and-sort-in-python
Python sort() and sorted(): Sort Lists, Strings & Dicts
2 weeks ago - The Python sort() function is used to sort elements in a list. By default, it arranges elements in ascending order and modifies the original list in-place. For example, with numbers = [5, 2, 7, 1], calling numbers.sort() changes it to [1, 2, 5, 7]. The sort() function also allows sorting in ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-sort-method
Python List sort() Method - GeeksforGeeks
December 20, 2025 - Python · a = [4, 1, 3, 2] a.sort() print(a) Output · [1, 2, 3, 4] Explanation: a.sort() arranges the list elements in increasing order, a is updated with the sorted values. lst.sort(key=None, reverse=False) Parameters: key (optional): A function ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Sort a List of Numeric Strings in Python | note.nkmk.me
January 31, 2024 - In Python, you can sort a list with the sort() method or the sorted() function. Sort a list, string, tuple in Python (sort, sorted) This article explains how to sort a list of numeric strings and a li ...
🌐
Python Tutorial
pythontutorial.net › home › python basics › python sort list
To sort a list, you use the sort() method - Python Tutorial
March 26, 2025 - This tuorial shows you how to use the Python List sort() method to sort a list e.g. a list of numbers and a list of strings.
🌐
TechBeamers
techbeamers.com › python-sort-a-list-of-numbers
Python Program: How to Sort a List of Numbers
November 30, 2025 - Here, the key parameter in the sorted() function is a lambda function that returns the remainder when each number is divided by 5. You can customize the lambda function based on your specific sorting criteria.
🌐
Mimo
mimo.org › glossary › python › list-sort()
Python List sort(): Master Data Sorting | Learn Now
# Creating a list of numbers and sorting them numbers = [5, 2, 9, 1] numbers.sort() # Sorts the numbers in ascending order print(numbers) # Outputs: [1, 2, 5, 9] You can sort the list in descending order by passing reverse=True as an argument: ... This method sorts the list based on the specified ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sort-numeric-strings-in-a-list
Sort Numeric Strings in a List - Python - GeeksforGeeks
July 11, 2025 - For example, if we have: a = ["10", "2", "30", "4"] then the expected output should be: ["2", "4", "10", "30"] because numerically, 2 < 4 < 10 < 30. We use Python's built-in sorted() function along with the key=int parameter as this converts ...
🌐
Real Python
realpython.com › videos › sorting-numbers-sorted
Sorting Numbers With sorted() (Video) – Real Python
00:42 The sorted() function is one of Python’s builtins, so it will always be available in a regular installation of Python. Second, because you just passed in numbers, the list, sorted() worked in its default mode and sorted these in ascending ...
Published   January 1, 2020
🌐
Reddit
reddit.com › r/learnpython › how do i sort the numbers in a given string?
r/learnpython on Reddit: How do I sort the numbers in a given string?
September 11, 2022 -

EX: string="abcd7efgh14ijkl5mn3op"

how can i sort it to be "abcd3efgh5ijkl7mn14op"

Top answer
1 of 6
20
Maybe like this: import re string = "abcd7efgh14ijkl5mn3op" parts = re.split(r"(\d+)", string) # ['abcd', '7', 'efgh', '14', 'ijkl', '5', 'mn', '3', 'op'] parts[1::2] = sorted(parts[1::2], key=int) result = "".join(parts) print(result) The string is split into parts by numbers in it. \d means a digit. + means one or more. Because it's put into capture group (), the separators (numbers) are included into parts. r before "" means raw string, so \d is treated as two separate characters by Python interpreter, not single symbol, like \n (new line) symbol, for example. Then each second part is sorted by using by using its integer form to compare against other parts. The sorted parts are put in place of old parts (each second part). In the end, parts are joined back using empty string as the separator. The result is then to be printed. Check "Change a Range of Item Values" on https://www.w3schools.com/python/python_lists_change.asp https://docs.python.org/3/library/functions.html#sorted https://docs.python.org/3/library/stdtypes.html?highlight=str%20join#str.join https://docs.python.org/3/library/re.html?highlight=re%20split#re.split More on RegEx on https://www.w3schools.com/python/python_regex.asp https://docs.python.org/3/howto/regex.html#regex-howto https://regex101.com/ (select Python flavor)
2 of 6
17
This is challenging for a couple of reasons. For one, you aren’t sorting the digits, you are sorting series of consecutive digits (ie 14 was sorted, not 1 and 4. I think regex would be helpful to find the groups of digits, then sort them and replace the groups with the right number.
🌐
Kanaries
docs.kanaries.net › topics › Python › python-sort-list
Python Sort: Complete Guide to sorted(), list.sort(), and Custom Sorting – Kanaries
February 11, 2026 - # Numeric sorting integers = [42, -7, 0, 93, 15] print(sorted(integers)) # [-7, 0, 15, 42, 93] floats = [3.14, 2.71, 1.41, 9.81] print(sorted(floats)) # [1.41, 2.71, 3.14, 9.81] # String sorting (lexicographic order) words = ['zebra', 'apple', 'mango', 'banana'] print(sorted(words)) # ['apple', 'banana', 'mango', 'zebra'] # Mixed types cause errors in Python 3 mixed = [3, 'apple', 1.5] # sorted(mixed) # TypeError: '<' not supported between instances · For numeric strings that should sort numerically rather than alphabetically, convert them during sorting: # String numbers sort alphabetically string_nums = ['10', '2', '100', '21'] print(sorted(string_nums)) # ['10', '100', '2', '21'] # Convert to int for numeric sorting print(sorted(string_nums, key=int)) # ['2', '10', '21', '100']
Top answer
1 of 3
2

You could combine your input to a single line to improve ease of use. For example,

storage = input("Enter values separated by spaces:")
storage = [int(x) for x in storage.split()]

This way you have the entire list of input and can avoid having to have the user enter the number of input, and avoid having to declare the num variable at all.

However, you probably also want to include some form of input validation or throw a meaningful error as right now if your user was to enter non-integers, your program would simply crash and output a vague error that the user would probably have a difficult time understanding.

try: 
    storage = [int(x) for x in test.split()]
except ValueError:
    print("Non-integers in input!")

Alternatively, you could check if all the values are numeric and if not, have the user re-enter their input.

As for your sorting algorithm, if you don't want to use Python's implemented sort() or sorted(), you should research more efficient algorithm's such as quick sort or even implement the bubble sort that you learned.

Currently, your min_sort algorithm finds the minimum value in the list, which is O(n) then removes that element from the list (separately from the search) which is again O(n). This is extremely wasteful as you may end up searching through the entire list again and again (n times), so it would be better to use a more efficient sorting algorithm or at least recognize that we don't need to pass through the entire list twice on each iteration, just pass through the list once and keep track of the minimum value. You could do this by writing your own function like:

def find_remove_min(nums):
    """Returns the minimum number and the list without the min number"""
    if nums:
        min_index = 0
        for i in range(1, len(nums)):
            if nums[i] < nums[min_index]:
                min_index = i
        return nums[min_index], nums[:min_index] + nums[min_index+1:]

Then you could do something like,

while storage:
    min_num, storage = find_remove_min(storage)
    result.append(min_num)

which would be more readable and efficient imo.

2 of 3
2

The built-in sorted(), which uses Timsort, is always preferable, but since you said you were learning bubble sort, I would stick with it even though it is too slow and mutate the input list instead of creating a new one.

numbers = input("Enter numbers separated by a comma: ")
numbers = [int(n) for n in numbers.split(',')]

end = len(numbers) - 1

while end != 0:

    for i in range(end):
        if numbers[i] > numbers[i + 1]:
            numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i]

    end = end - 1

Running it:

Enter numbers separated by a comma: 3, 0, 1, 4, 2
[0, 1, 2, 3, 4]
>>> 
🌐
MAC Address Lookup
aruljohn.com › home › articles › code
How to Use Python list.sort() and sorted() Functions
December 6, 2024 - In Python, you can sort a list or array in two ways. The first is a built-in function sorted() and the second is a list method list.sort(). ... The sorted() function takes a list as argument and returns a sorted list. The original list is unchanged.