Reorder sorting and writing.
Update and expand documentation to match D style.