Python program to sort string in custom order

Sorting strings in a custom order requires defining a special key function. In this problem, we need to sort an alphanumeric string with specific priority rules for different character types.

Sorting Rules

The custom sorting follows this priority order:

  • All sorted lowercase letters come first

  • All sorted uppercase letters come second

  • All sorted odd digits come third

  • All sorted even digits come last

For example, if the input is "HeLlo1234", the output will be "eloHL1324".

Custom Key Function

We create a key function that assigns priority codes to different character types ?

def custom_key(c):
    code = 0
    
    if c.isupper():
        code = 10 ** 3  # Uppercase letters: 1000 + ASCII
    elif c.isdigit():
        code = 10 ** 6  # Digits: 1000000 + ASCII
        if ord(c) % 2 == 0:
            code = 10 ** 9  # Even digits: 1000000000 + ASCII
    # Lowercase letters: 0 + ASCII (highest priority)
    
    return code + ord(c)

# Test the key function
print("Key for 'e':", custom_key('e'))
print("Key for 'H':", custom_key('H'))
print("Key for '1':", custom_key('1'))
print("Key for '2':", custom_key('2'))
Key for 'e': 101
Key for 'H': 1072
Key for '1': 1000049
Key for '2': 1000000050

Complete Solution

Here's the complete function to sort a string in custom order ?

def custom_key(c):
    code = 0
    
    if c.isupper():
        code = 10 ** 3
    elif c.isdigit():
        code = 10 ** 6
        if ord(c) % 2 == 0:
            code = 10 ** 9
    
    return code + ord(c)

def sort_custom_order(s):
    sorted_chars = sorted(s, key=custom_key)
    return ''.join(sorted_chars)

# Test with example
s = "HeLlo1234"
result = sort_custom_order(s)
print(f"Input: {s}")
print(f"Output: {result}")
Input: HeLlo1234
Output: eloHL1324

How It Works

The sorting mechanism assigns different priority ranges:

Character Type Priority Code Example
Lowercase letters 0 + ASCII 'e' ? 101
Uppercase letters 1000 + ASCII 'H' ? 1072
Odd digits 1000000 + ASCII '1' ? 1000049
Even digits 1000000000 + ASCII '2' ? 1000000050

Testing with Different Inputs

def custom_key(c):
    code = 0
    
    if c.isupper():
        code = 10 ** 3
    elif c.isdigit():
        code = 10 ** 6
        if ord(c) % 2 == 0:
            code = 10 ** 9
    
    return code + ord(c)

def sort_custom_order(s):
    return ''.join(sorted(s, key=custom_key))

# Test cases
test_cases = ["HeLlo1234", "aZ5B2c9", "Python3"]

for test in test_cases:
    result = sort_custom_order(test)
    print(f"{test} ? {result}")
HeLlo1234 ? eloHL1324
aZ5B2c9 ? acBZ592
Python3 ? honytPh3

Conclusion

Custom string sorting uses a key function that assigns priority codes to different character types. The sorted() function with a custom key enables complex sorting patterns based on character properties and ASCII values.

Updated on: 2026-03-26T15:43:33+05:30

978 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements