Archive for March, 2012

Ruby: fine grained sorting

If we had this array: ["abc", "aaa", "add", "bcc", "baa"] sorting normally we would get:

["aaa", "abc", "add", "baa", "bcc"]

Let’s say we wanted to sort by the second letter, we could do this using the sort_by method:

a.sort_by { |a| a[1] }
["baa", "aaa", "abc", "bcc", "add"]

Now lets see a more complex example, if we wanted to sort an email list:

earl@company.com
brett@random.com
abel@company.com
forest@random.com

First by the host name and then by the user name we can use the sort_by method like this:

sorted_emails = emails.sort_by { |e| [ e[/@.*/], e[/.*@/] ] }

We pass a block to sort_by with the ‘rules’ we want to sort by, we are using a regular expression to express how we want to sort, they are /@.*/ which matches everything after the at sign and /.*@/ which matches everything before.

Finally we can apply the same idea to uniq so that we can get unique data based on a pattern, I use this in Dirfuzz to filter the results and avoid duplicates when I have duplicate results that aren’t exactly the same.

@rel_links.sort.uniq { |link| link[/(?:\/\w+)+/] }

This regexp will allow me to get rid of duplicates with this data:

/community/lists/
/community/lists
/community/lists/#clp

This would stay the same with a simple uniq, but passing a block with that regexp will get rid of the duplicates.

, , , , , ,

Leave a comment

%d bloggers like this: