String Character Sorter (Ascending/Descending)

  • Share this:

Code introduction


This function sorts the characters in a string based on the provided string and a boolean value. If descending is True, it sorts in descending alphabetical order; if False, it sorts in ascending order.


Technology Stack : String, sorting, boolean value

Code Type : String sort function

Code Difficulty : Intermediate


                
                    
def aordesc(a_string, descending=True):
    if not isinstance(a_string, str):
        raise ValueError("Input must be a string.")
    return sorted(a_string, reverse=descending)