CodeSampleX

Sample

semver 7.8.5: sort and reverse sort semantic versions with sort, rsort, and compareBuild

Verified sample for npm semver 7.8.5: sort and reverse sort semantic versions with sort, rsort, and compareBuild. The contract ran on node 22 · linux…

sha256:26f645febc9c2deea5d00a0918ae7444800698ffcea15760bc7be42ee34375d2

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
2
Signing keys that built it
2
Declared environment windows 11 x64 npm

Verification-run environments

Environment Contract Stages Run
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-19
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-09-07

Case

HOW
Goal
sort and reverse sort semantic versions with sort, rsort, and compareBuild
Packages
Symbols
  • compareBuild
  • rsort
  • sort
Created
2026-08-19T10:55:07Z

Contract

  1. semver.sort sorts an array of version strings in ascending semver order
  2. semver.rsort sorts an array of version strings in descending semver order
  3. semver.compareBuild compares two versions distinguishing build metadata
  4. semver.sort and semver.rsort mutate arrays in place and return the sorted array

Files

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

Download the source artifact (tar.gz)

Source

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: sort and reverse sort semantic versions with sort, rsort, and compareBuild
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/semver@7.8.5
Demonstrate these symbols/APIs:
  - compareBuild
  - rsort
  - sort
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@11.8.0
  - runtime: node@24.13.1

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:4cc337b6e3f229dece26820c20f90e46825da93e5d1da820aaa089c8d215236f","contract":["semver.sort sorts an array of version strings in ascending semver order","semver.rsort sorts an array of version strings in descending semver order","semver.compareBuild compares two versions distinguishing build metadata","semver.sort and semver.rsort mutate arrays in place and return the sorted array"],"goal":"sort and reverse sort semantic versions with sort, rsort, and compareBuild","kind":"HOW","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["compareBuild","rsort","sort"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","os":"windows","osVersionBucket":"11","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["compareBuild","rsort","sort"],"verifierAdapter":"node-typescript@1"}
index.mjs
import semver from 'semver';

/**
 * Sorts an array of semantic version strings in ascending order.
 *
 * @param {string[]} list - Array of version strings to sort.
 * @param {object|boolean} [optionsOrLoose] - Options or loose flag.
 * @returns {string[]} The sorted array (mutated in place).
 */
export function sortVersions(list, optionsOrLoose) {
  return semver.sort(list, optionsOrLoose);
}

/**
 * Sorts an array of semantic version strings in descending order.
 *
 * @param {string[]} list - Array of version strings to sort.
 * @param {object|boolean} [optionsOrLoose] - Options or loose flag.
 * @returns {string[]} The reverse-sorted array (mutated in place).
 */
export function rsortVersions(list, optionsOrLoose) {
  return semver.rsort(list, optionsOrLoose);
}

/**
 * Compares two versions including build metadata.
 *
 * @param {string} v1 - First version string.
 * @param {string} v2 - Second version string.
 * @param {object|boolean} [optionsOrLoose] - Options or loose flag.
 * @returns {number} 0 if v1 == v2, 1 if v1 > v2, -1 if v1 < v2.
 */
export function compareWithBuild(v1, v2, optionsOrLoose) {
  return semver.compareBuild(v1, v2, optionsOrLoose);
}

export { semver };
package-lock.json
{
  "name": "sample-semver-sort",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-semver-sort",
      "version": "1.0.0",
      "dependencies": {
        "semver": "7.8.5"
      }
    },
    "node_modules/semver": {
      "version": "7.8.5",
      "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz",
      "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==",
      "license": "ISC",
      "bin": {
        "semver": "bin/semver.js"
      },
      "engines": {
        "node": ">=10"
      }
    }
  }
}
package.json
{
  "name": "sample-semver-sort",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "semver": "7.8.5"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "sort and reverse sort semantic versions with sort, rsort, and compareBuild",
  "kind": "HOW",
  "packages": [
    "pkg:npm/semver@7.8.5"
  ],
  "symbols": [
    "compareBuild",
    "rsort",
    "sort"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@11.8.0",
    "runtime": "node@24.13.1"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import semver from 'semver';
import { sortVersions, rsortVersions, compareWithBuild } from '../index.mjs';

// 1. semver.sort sorts an array of version strings in ascending semver order
const versionsAsc = ['1.2.3', '0.1.2', '1.3.0', '1.2.3-alpha'];
const sortedAsc = semver.sort([...versionsAsc]);
assert.deepStrictEqual(sortedAsc, ['0.1.2', '1.2.3-alpha', '1.2.3', '1.3.0']);
assert.deepStrictEqual(sortVersions([...versionsAsc]), ['0.1.2', '1.2.3-alpha', '1.2.3', '1.3.0']);

// 2. semver.rsort sorts an array of version strings in descending semver order
const versionsDesc = ['1.2.3', '0.1.2', '1.3.0', '1.2.3-alpha'];
const sortedDesc = semver.rsort([...versionsDesc]);
assert.deepStrictEqual(sortedDesc, ['1.3.0', '1.2.3', '1.2.3-alpha', '0.1.2']);
assert.deepStrictEqual(rsortVersions([...versionsDesc]), ['1.3.0', '1.2.3', '1.2.3-alpha', '0.1.2']);

// 3. semver.compareBuild compares two versions distinguishing build metadata
assert.strictEqual(semver.compare('1.0.0+build1', '1.0.0+build2'), 0);
assert.strictEqual(semver.compareBuild('1.0.0+build1', '1.0.0+build2'), -1);
assert.strictEqual(semver.compareBuild('1.0.0+build2', '1.0.0+build1'), 1);
assert.strictEqual(semver.compareBuild('1.0.0+build1', '1.0.0+build1'), 0);
assert.strictEqual(compareWithBuild('1.0.0+1', '1.0.0+2'), -1);
assert.strictEqual(semver.compareBuild('1.2.0', '1.1.0'), 1);
assert.strictEqual(semver.compareBuild('1.0.0-rc.1', '1.0.0'), -1);

// 4. semver.sort and semver.rsort mutate arrays in place and return the sorted array
const arr1 = ['2.0.0', '1.0.0'];
const ret1 = semver.sort(arr1);
assert.strictEqual(ret1, arr1);
assert.deepStrictEqual(arr1, ['1.0.0', '2.0.0']);

const arr2 = ['1.0.0', '2.0.0'];
const ret2 = semver.rsort(arr2);
assert.strictEqual(ret2, arr2);
assert.deepStrictEqual(arr2, ['2.0.0', '1.0.0']);

console.log('PASS: semver sort, rsort, and compareBuild contracts verified');

Origin Seeder

anonymous