Why are citations not ordered by appearance in the text?

Hi, several months ago, someone in this forum helped develop a new citation style that I could use for work, allowing additional information to be displayed.

What I am finding is that when I have a longer bib file (lets say 15 items or so) Typst renders the citation out of order: If I reference sources in a non-linear order compared to how it is in the bib file (e.g., cite [3] may show before [1]), Typst might assign numbers based on the order in which it sees the citations during the rendering pass, not necessarily their order in the text. Alternatively, it may employ a different method. It doesn’t seem consistent. Or at least I can not figure it out.

Can anyone help? I have given a small example. This one will work because it is only two bibs, but if you have your own and want to add to it, you will see what happens.

On my main.py page, I include this.

#let csl = ```xml
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0" demote-non-dropping-particle="sort-only">
  <info>
    <title></title>
    <id></id>
  </info>
  <macro name="author">
    <names variable="author">
      <name sort-separator=" " delimiter=", " initialize-with=". " delimiter-precedes-last="never" name-as-sort-order="all"/>
    </names>
  </macro>
  <citation>
    <sort>
      <key variable="citation-number"/>
    </sort>
    <layout prefix="[" suffix="]" delimiter=", ">
      <text variable="citation-number"/>
    </layout>
  </citation>
  <bibliography second-field-align="flush">
    <sort>
      <key variable="author"/>
      <key variable="title"/>
    </sort>
    <layout suffix=".">
      <text variable="citation-number" suffix=". " />
      <text macro="author" suffix=". " />
      <text variable="title" font-style="italic" suffix=". " />
      <text variable="container-title" suffix=", " />
      <text variable="contributor" suffix=", " />
      <text variable="version" suffix=", " />
      <text variable="volume" suffix=", " />
      <text variable="issue" suffix=", " />
      <text variable="publisher" suffix=", " />
      <text variable="publisher-place" suffix=", " />
      <date variable="issued">
        <date-part name="year" suffix=", " />
      </date>
      <text variable="page" suffix=", " />
      <text variable="annote" prefix="(" suffix=") "/>
      <text variable="URL" prefix="URL: " />
    </layout>
  </bibliography>
</style>
```.text

I also use markdown for most of the body content

#import "@preview/cmarker:0.1.3"
#import "toolkit.typ": *
  

  // Pull in the markdown document
  #counter(page).update(1)   // sets the page number to be one (1)
  #set enum(indent: 1em)     // set the indent for item list numbering

#cmarker.render(
  read("input.md"),
  raw-typst: true,         // ← Must be here, after markdown and before html tuple
  blockquote: leftbar,
  scope: (
    image: (path, alt: none) => image("assets/" + path, alt: alt),
  ),
  html: (
    cite: ("escapable-raw-text", (attrs, body) => ref(label(body))),
    // other HTML handlers go here
  ),
)

So the body may look like

Guardians ad Litem in Pennsylvania serve in various legal contexts, including dependency, custody, juvenile delinquency, and even probate matters involving minors or incapacitated individuals <cite>231PaCode</cite> and <cite>237PaCode</cite>. This report focuses specifically on GALs in dependency and high-conflict custody proceedings, where their role is most consistently tied to the direct protection of children's welfare.


### Expected Activities in Practice

GALs are not meant to be symbolic appointees. Their role is active, investigative, and central to judicial decision-making in child welfare and custody proceedings. In principle, a GAL’s duties extend far beyond reviewing case files or attending court hearings. Based on statutory obligations and nationally accepted best practices <cite>hhs1988gal</cite>, GALs are expected to engage in the following activities:

and the bib file may look like

@misc{237PaCode,
  title = {237 {{Pa}}. {{Code Rule}} 1154. {{Duties}} of {{Guardian Ad Litem}}.},
  urldate = {2025-04-16},
  howpublished = {https://www.pacodeandbulletin.gov/Display/pacode?file=/secure/pacode/data/237/chapter11/s1154.html\&d=reduce},
  file = {C:\Users\mpinsley\Zotero\storage\EFFYQWL8\pacode.html}
}

@misc{hhs1988gal,
  author       = {{U.S. Department of Health and Human Services}},
  title        = {National Evaluation of the Impact of Guardian Ad Litem in Child Abuse or Neglect Judicial Proceedings},
  year         = {1988},
  month        = {jun},
  institution  = {{U.S. Department of Health and Human Services}},
  note         = {Government report},
  keywords     = {guardian ad litem, child abuse, judicial proceedings, evaluation, HHS},
  url          = {https://www.ojp.gov/pdffiles1/Digitization/114331NCJRS.pdf}
}

It appears that you set your bibliography to sort by author first and by title second in your CSL, I wonder if that’s not what you see?

Maybe you could provide a minimal reproducible example where this fails? A simple, single-file document which produces unexpected results, so we can test by ourselves. (For example, I don’t know what toolkit.typ is, nor where you are placing bibliography, so this is not a minimal reproducible example.)