Macro to select all materials and sort them alphabetically?

I know I can right click > select all > sort alphabetically but when I have a large material list it gets quite annoying and slow to do this so was wondering if it’s possible to do this via a macro - or if it’s possible to do other things pertaining to the material list via macro like merging identical materials.

Thanks!