Add sorting by similarity to all pinned tabs.

Also add comments for many functions.