Ruby sorting is really easy, lets see some examples. If we had this array: ["abc", "aaa", "add", "bcc", "baa"] sorting normally we would get:
|
1 |
["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:
|
1 2 |
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:
|
1 2 3 4 |
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:
|
1 |
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.
|
1 |
@rel_links.sort.uniq { |link| link[/(?:/w+)+/] } |
This regexp will allow me to get rid of duplicates with this data:
|
1 2 3 |
/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 Reply