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

Recommended Answers

All 2 Replies

your question was answered in devshed

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']
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.