Extension:SimpleSort

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.svg
SimpleSort

Release status:Extension status experimental

ImplementationTemplate:Extension#type Parser function
DescriptionTemplate:Extension#description A simple extension to allow sorting of elements within an input string.
Author(s)Template:Extension#username Clark Verbrugge (clumptalk)
Latest versionTemplate:Extension#version 1.0.0
MediaWikiTemplate:Extension#mediawiki 1.26+
PHPTemplate:Extension#php 5.4+
Database changesTemplate:Extension#needs-updatephp No
LicenseTemplate:Extension#license Creative Commons Zero v1.0 Universal
Download
Hooks usedTemplate:Extension#hook
ParserFirstCallInitManual:Hooks/ParserFirstCallInit

Translate the SimpleSort extension if it is available at translatewiki.net

Check usage and version matrix.

The SimpleSort extension allows you to sort a list of items, contained in the same string.

Usage[edit]

Once installed you can use the #simplesort template invocation. It takes 1 or 2 arguments, depending on whether you are happy with the defaults or if you want to customize the sorting. The 1-argument form assumes a comma-separated list of items (ignoring and discarding whitespace around the commas), and sorts them in ascending order, case-insensitive, following php's "natural" sorting order:

  • {{#simplesort:a, LIST,of, Things,to , sort, maybe with, num1, num11, num2}}
    • This produces output a,LIST,maybe with,num1,num2,num11,of,sort,Things,to

Optionally, you may specify as a first argument one or more options, each separated by whitespace. Available options are:

  • desc Use descending rather than ascending order.
  • alpha Use regular alphabetic ordering.
  • num Use regular numeric ordering.
  • case Use a case-sensitive comparison.
  • insep="X" Use X as an item separator to recognize items. Whitespace around X will still be ignored and discarded.
  • outsep="X" Use X as an item separator in the sorted output. The default is to use the same string as insep
  • stoponblank Discards all list items after the first blank entry.

For example,

  • {{#simplesort:desc case|a, LIST,of, Things,to , sort, maybe with, num1, num11, num2}}
    • to,sort,of,num11,num2,num1,maybe with,a,Things,LIST
  • {{#simplesort:num insep=" "|3 4 2 1 8 9 2 6 5 }}
    • 1 2 2 3 4 5 6 8 9
  • {{#simplesort:insep="potato" outsep=", "|rutabaga potato onionpotatorat}}
    • onion, rat, rutabaga

Keyed Sorting[edit]

The sorting order may also be given by sorting one list, applying the resulting ordering to a second list. For this you need to use the keyed option as (one of) the first argument(s) to #simplesort, and to supply two lists. The sort order generated by the first list is then used to reorder the second list, and only the second list is output. For example,

  • {{#simplesort:keyed|b, c, a|Veronica, Jughead, Archie}}
    • Archie,Veronica,Jughead

Other options described above can also be used in conjunction with keyed. For example,

  • {{#simplesort:keyed outsep=" loves "|b, c, a|Veronica, Jughead, Archie}}
    • Archie loves Veronica loves Jughead

Note, however, that the input separators must be the same for both lists, and (of course) the output separator is only applied to the second list. If the first list is shorted than the second then remaining items in the second list are discarded. If the first list is longer than the second then blank entries will be used to fill out the output.

Installation[edit]

  • Download and place the file(s) in a directory called SimpleSort in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
    wfLoadExtension( 'SimpleSort' );
    
  • YesY Done - Navigate to Special:Version on your wiki to verify that the extension is successfully installed.