Need sorting method which can sort list containing alphanumeric and special character both and also it should consider special character sorting

Need sorting method which can sort list containing alphanumeric and special character both and also it should consider special character sorting.

Hi Krishna,

The Sort List keyword i mentioned previously just calls the default .sort() method on a list (python list)

If you just want to change the sort direction you can use the Evaluate keyword something like this:

${sorted}=    Evaluate    ${yourlist.sort(reverse=True)}

If you need a custom sort method, you’ll probably need to write one in python code, this might help on how to use the Python List sort() Method

Also you’ll probably want to refer to Creating keywords for creating python keywords for robot framework.

Hope that helps,

Dave.

2 Likes

Hello @krishna2,

To sort a list containing alphanumeric and special characters while considering special character sorting, you can use a custom sorting method. Here’s a Python function that accomplishes this:

import re

def custom_sort(lst):
# Define a custom sorting key function
def sorting_key(item):
# Use regular expressions to split the item into alphanumeric and special character parts
parts = re.split(r’([^\w])', item)
# Convert alphanumeric parts to lowercase for case-insensitive sorting
alphanumeric_parts = [part.lower() for part in parts if part.isalnum()]
# Join alphanumeric and special character parts back together
return β€˜β€™.join(alphanumeric_parts), β€˜β€™.join(parts)

# Sort the list using the custom sorting key function
sorted_lst = sorted(lst, key=sorting_key)
return sorted_lst

Example usage

my_list = [β€œc”, β€œb”, β€œa”, β€œ10”, β€œ9”, β€œ8”, β€œ!”, β€œ@”, β€œ#”]
sorted_list = custom_sort(my_list)
print(sorted_list)

This function defines a custom sorting key function that splits each item into alphanumeric and special character parts using regular expressions. It then converts alphanumeric parts to lowercase for case-insensitive sorting and joins the parts back together for sorting. Finally, it sorts the list using the custom sorting key function.

You can adjust the function as needed to suit your specific requirements and integrate it into your Robot Framework test cases or scripts.

Best Regards,