Search the UDF forum, you'll find hundreds of functions contributed by board members that will assist for these sort of tasks.

For example Qsort() is a fast array sorter (much faster than a bubble sort) and RemoveDups() is just one of a number of UDFs for removing duplicates.

Spend a little time digging around in the UDF forum and you'll find the odd golden nugget.