0

Please let me know how to sort the list of String in either ascending / descending order without considering special characters and case.
ex: list1=['test1_two','testOne','testTwo','test_one']
Applying the list.sort /sorted method results in sorted list ['test1_two', 'testOne', 'testTwo', 'test_one']
but the without considering the special characters and case it should be
['testOne','test_one', 'test1_two','testTwo'] OR ['test_one','testOne','testTwo', 'test1_two' ]

list.sort /sorted method sorts based on the ascii value of the characters but Please let me knwo how do i achieve my expected one

3
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by sneekula
0

A helper function will do:

'''sort_alpha_only1.py
sort only the alphabetic part of a word
has:
['test1_two', 'testOne', 'testTwo', 'test_one']
wants:
['testOne', 'test_one', 'test1_two', 'testTwo']
'''

def alpha_only(word):
    '''
    helper function to sort only the alphabetic part of a word
    '''
    word = "".join(c for c in word if c.isalpha())
    return word.lower()

list1 = ['test1_two','testOne','testTwo','test_one']
list2 = sorted(list1, key=alpha_only, reverse=False)
print(list2)

'''
['testOne', 'test_one', 'test1_two', 'testTwo']
'''
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.