~adnano/kiln-discuss

1

Dir.sort's comment

Details
Message ID
<eZLvj2pzIAqbC2SOCcIyU5toNdEwRSoWAtE8eooIUDe5cWDfza1Xej0fNZOIpXkn7noPgrwlok35oUP3O4VQiWq7HeIJSXgRSXXmhaHxH1s=@protonmail.com>
DKIM signature
pass
Download raw message
The comment is -

// sort sorts the directory's pages by weight, then date, then filepath.
func (d *Dir) sort()


But the code does it in the opposite order.

	sort.Slice(d.Pages, func(i, j int) bool {
		pi, pj := d.Pages[i], d.Pages[j]
		return pi.FilePath < pj.FilePath
	})

	sort.SliceStable(d.Pages, func(i, j int) bool {
		pi, pj := d.Pages[i], d.Pages[j]
		return pi.Date.After(pj.Date)
	})

	sort.SliceStable(d.Pages, func(i, j int) bool {
		pi, pj := d.Pages[i], d.Pages[j]
		return pi.Weight < pj.Weight
	})

Am I missing something?
Details
Message ID
<CCFBC72PF7HY.3VH5QRRBLDAWV@nitro>
In-Reply-To
<eZLvj2pzIAqbC2SOCcIyU5toNdEwRSoWAtE8eooIUDe5cWDfza1Xej0fNZOIpXkn7noPgrwlok35oUP3O4VQiWq7HeIJSXgRSXXmhaHxH1s=@protonmail.com> (view parent)
DKIM signature
pass
Download raw message
On Mon Jun 28, 2021 at 10:05 AM EDT, nadashin wrote:
> The comment is -
>
> // sort sorts the directory's pages by weight, then date, then filepath.
> func (d *Dir) sort()
>
> But the code does it in the opposite order.

The comment states the precedence of the variables: weight has the
highest precedence, followed by date, and then filepath.

In order to achieve this, we first sort by the lowest-precedence
variable (filepath) and work our way to the highest-precedence variable
(weight). We use an unstable sort for the first sort because every page
should have a unique filepath, and stable sorts for the rest to retain
the order from the previous sorts.
Reply to thread Export thread (mbox)