Sorting practice exercises?

Thoughts on sorting practice exercises?

  • Some tracks sort by name (alphabetically)
  • Some tracks sort by difficulty (then name)
  • Some tracks take the syllabus tree ordering into account and use that for sorting
  • Some tracks sort by date-created (the default when you use the configlet to add new exercises)

The first two can be detected pretty easily to flag tracks that are not sorted alphabetically and not sorted by difficulty to do a sorting-cleanup. Do you want a PR on your track to sort the exercises?

If you want to sort by difficulty then name, you can use this:

sorted=$( jq '
  (.exercises.practice |= sort_by(.slug)) |
  .exercises.practice |= sort_by(.difficulty)' config.json )
printf '%s\n' "${sorted}" > config.json


Sorting practice exercises?

1 Like

There’s a distinction to be made between sorting by numeric difficulty versus the difficulty on the website (easy, medium, or hard).

I’m a strong proponent of sorting by displayed, not numeric, difficulty. Arturo, CoffeeScript, LFE, Pyret, Racket, Rescript, and VimScript are sorted that way. I solo maintain Arturo and Pyret so I’ll add a CI check there to ensure exercises are sorted correctly.

For a while, I maintained CFML, D, Emacs Lisp, and Red. During that time, I largely sorted their exercises by displayed difficulty as well, but it’s up to the current maintainers what they want to do. CFML is notably unmaintained now so if we don’t have a recommended default sort order, maybe pick one for that track.

1 Like

Hrm. A better starting point here might be a GHA (http://github.com/exercism/github-actions/) that checks if exercises are sorted with a sort-selector (eg one-or-more of “difficulty”, “displayed-difficulty”, “slug”). I might work on that first then tracks could opt into that GHA.

1 Like

It’s probably worth then documenting the commonly used sorting methods in the Exercism docs so maintainers have a reference when choosing a sort strategy.

Sorting PR which includes an update to the exercism/github-actions README:

The ordering input must be one of the following strings.

  • “slug”: sort alphabetically by the slug
  • “bucket”: sort by the difficulty “bucket” or "displayed difficulty (easy, medium, hard)
  • “bucket-then-slug”: sort by the difficulty “bucket” then by slug
  • “difficulty”: sort by the difficulty value (1, 2, 3, … 10)
  • “difficulty-then-slug”: sort by the difficulty then by slug
1 Like

On recent tracks, I have been sorting by numeric difficulty followed by case-insensitive name.

This way “Conway’s Game of Life” is not sorted under “g” and “D&D Character” is not sorted under “dn”

2 Likes

Yeah, good catch. I was assuming Isaac meant sorting alphabetically by exercise name, not slug. Slugs are likely more meaningful to maintainers than students.

I meant slug, which is used a fair bit for sorting :slight_smile:

I reworked the check to dump the slugs and use diff to compare sorted to unsorted … and made use of sort_by() with injected paths. This allows track maintainers to set any arbitrary sort keys, eg .difficulty, .name or .bucket, .slug. This makes the sorting more flexible (allowing arbitrary sorting) and more brittle (code injection, allows invalid path expressions).

Does case matter here? I didn’t add case-insensitive support. I don’t know if it’s needed. I could add a .lowername key if it’s helpful.

The proposed GHA is for checking if the exercises are sorted a particular way. Should there be an optional mode to then force that sort or is that out of scope? The original post mentioned sorting the exercises which is a separate action from checking if they are already sorted.

I’m fine with or without that option, but I figured I’d bring it up for clarity.

Sorting the exercises is not part of the plan here. IIRC jq and configlet format the JSON differently which makes auto sort-and-format a bit more involved.

1 Like

Okay, sounds good to me. Thanks

FYI this is something I like to add to an “add exercise” script: moonscript/bin/add-practice-exercise at 93a147a2791af38e1f5d539d766c5f37ec2f995c · exercism/moonscript · GitHub

If you know nim, this sorting could be defined directly (and optionally) in the config.json and configlet create would ideally insert new exercises in the correct place. configlet fmt and/or configlet lint could check it.

Yes, otherwise “ETL” sorts before “Eliud’s Eggs”, “ISBN Verifier” sorts before “Intergalactic Transmission” and “RNA Transcription” sorts before “Reverse String”

1 Like

Added .lowercase_name to the data.

1 Like

That’s a great idea! Can’t wait to add it to my track.

Merged. You should now be able to use it.

2 Likes

Works like a charm! @glennj rescued me with the script to actually sort the JSON entries, thanks everybody!

1 Like