Fix index sort order for IDs with differing digit counts - #22
Merged
Merged
Conversation
Documents were ordered by a byte comparison of the sort field, so IDs whose digit counts differ were placed in the wrong order: "10" sorts before "9". Digit runs are now compared by numeric value, and values that are numerically equal but written differently keep a byte comparison so the order stays total. The ID taken from a filename was also limited to the first three characters, so the default 4-digit filename format produced "000" for 0009-*.md and "0100-*.md" was read as "010". All leading digits are now used, whatever the width. Documents whose sort field is equal are ordered by filename, so the generated index no longer depends on the order the directory happens to be iterated in. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017bJFwP1iT8PKTQMWuMAYkZ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
draft <template> indexcould place0010before0009. Two defects caused this.1. The sort field was compared byte by byte.
sortDocumentsusedmem.order(u8, ...), so IDs written with different digit counts were ordered as text:"10"sorts before"9", and"1000"before"999".2. The ID taken from a filename was truncated to three characters.
extractDocumentMetareadfilename[0..3], but the defaultfilename_formatfor theadrtemplate is{{@id{4}}}-{{@title{kebab}}}.md. A document without an- ID:line therefore got"000"from0009-foo.mdand"010"from0100-foo.md, which is both a wrong value in the ID column and a wrong sort key.Observed before the fix, for filenames without an
- ID:line:and for IDs
9and10taken from the document body,0010-b.mdsorted ahead of0009-a.md.Changes
compareNatural, which compares runs of digits by numeric value and the surrounding text byte by byte. Values that are numerically equal but written differently ("9"and"009") fall back to a byte comparison so the ordering stays total. It is used for every string sort field, so@dateand@titlebenefit as well.extractDocumentMetanow takes all leading digits of the filename as the ID, whatever the width.Result
With IDs 8, 9, 10, 11, 100 in the document bodies and one document that has no
- ID:line (0012-no-id-line.md):Testing
compareNaturalacross digit widths and zero padding, numeric ordering insortDocuments, and the filename tiebreaker.zig build test --summary all: 64/64 tests passedzig fmt --checkreports no differencesREADME documents the natural ordering and the filename tiebreaker in the index sorting section.
🤖 Generated with Claude Code
https://claude.ai/code/session_017bJFwP1iT8PKTQMWuMAYkZ