jQuery tablesorter 2.0 - Roman Numeral Parser

# tablesorter

## Roman Numeral Parser

### Flexible client-side table sorting

Back to documentation

### Notes

- This parser (added v2.17.3) will convert roman numerals into their decimal equivalent so the table column can be sorted correctly.
- There are actually 3 separate parsers included with this script.
  - They are very similar, but were written to cover different use cases.
  - Refer to each in their separate sections below.
- This demo includes the stored roman numeral values within the table cells, toggle the view using the button directly above the table.

### "roman" parser

- This parser is optimized for columns that contain only roman numerals.
- In the demo below, this parser is used for the "Short" and "Long" columns.
- This parser has no option settings.

    $(function() {

    	$("table").tablesorter({
    		theme: 'blue',
    		widgets: ['zebra'],
    		headers: {
    			0 : { sorter: 'roman' },
    			1 : { sorter: 'roman' }
    		}
    	});

    });

### "roman-ignore" parser

This parser is designed to use the `roman_ignore` option to either:

#### Ignore The Last "X" Characters

For content that contains a roman number followed by an alphabetical character, such as "Ia" or "IIb", this parser can be set to ignore the last character (spaces are trimmed automatically):

    $(function() {

    	$("table").tablesorter({
    		theme: 'blue',
    		widgets: ['zebra'],
    		headers: {
    			2 : { sorter: 'roman-ignore' }
    		},
    		// roman numeral parser option
    		// ignore the last (1) character(s) in column 2 (zero-based index)
    		// the two zeros in the array are just placeholders ( [ , , 1 ] works as well )
    		roman_ignore: [ 0, 0, 1 ]

    	});

    });

#### Remove Non-Roman Numerals

For cells that contain a bit more complex layout, you can define a regular expression to ignore (remove) certain parts of the content.
The value obtained from the `roman_ignore` option array is used within a javascript replace function, so it can be either a regular expression or a string.
In this example (see the "Ignore regex" column in the demo below), content at the beginning of the cell is set to be ignored. This should leave the roman numeral string to be parsed by this script (spaces are trimmed automatically).

    $(function() {

    	$("table").tablesorter({
    		theme: 'blue',
    		widgets: ['zebra'],
    		headers: {
    			3 : { sorter: 'roman-ignore' }
    		},
    		// roman numeral parser option
    		// ignore any words at the beginning of column 3 (zero-based index) using a regular expression
    		// additionally, if all column content contains the same character to ignore, a string can be
    		// passed within this option, e.g. "Chapter "
    		// the three zeros in the array are just placeholders ( [ , , , /^(\w+\s)/ ] works as well )
    		roman_ignore: [ 0, 0, 0, /^(\w+\s)/ ]

    	});

    });

### "roman-extract" parser

- This parser will attempt to extract out a roman numeral block from the cell content.
- It's not perfect. If the content contains two blocks of roman numerals, they will be combined. For example,
  - If a cell contains `X plus VII`, the parser will extract out `XVII` and return a value of 17.
  - Or worse yet, if a cell contains `VI minus X`, the parser will extract out `VIX` which is not a valid roman numeral, so it will instead return the initial value of `VI minus X`. If this is the case, use the "roman-ignore" parser instead.

    $(function() {

    	$("table").tablesorter({
    		theme: 'blue',
    		widgets: ['zebra'],
    		headers: {
    			4 : { sorter: 'roman-extract' }
    		}
    	});

    });

# Demo

parsed values within the column

Pure Roman Numerals | Ignore Non-Roman Numerals | Extract Roman Numerals
--- | --- | ---
Short | Long | Ignore last (1) character * | Ignore regex (/^(\w+\s)/) | Extract
I | MDLXXXV | iiia | Mark I | 2000 XXVII Sydney
MXI | MDCLXVI | iiib | Mark IV | 2012 XXX London
XII | MMDCCCLVIII | ia | Mark V | 2020 XXXII Tokyo
CXI | MMCCI | va | Mark VII | 2004 XXVIII Athens
XXI | MDCCCXL | vi b | Mk III | 1980 XXII Moscow
XIII | MMMCXXIX | iva | Mod X | 1972 XX Munich
V | DLXXVII | via | Mod IV | 2016 XXXI Rio de Janeiro
X | MDCLXV | xia | Mk VIII | 1996 XXVI Atlanta
XI | MDXVIII | xiiz | Mod XII | 1976 XXI Montreal
CLXI | MDCCCLX | xd | Mk 0 | 1992 XXV Barcelona
C | CCCXCI | iiic | Mk VI | 1988 XXIV Seoul
LV | MLXXX | xxf | Mk II | 1984 XXIII Los Angeles
IX | DCCLVII | lig | Mod L | 2008 XXIX Beijing

* Ignoring the last letter (set number to ignore in `roman_ignore` option array; notice that "vi b" sorts before "via" - spaces do matter!)

# Page Header

    <!-- blue theme stylesheet with additional css styles added in v2.0.17 -->
    <link rel="stylesheet" href="../css/theme.blue.css">
    <!-- tablesorter plugin; requires jQuery -->
    <script src="../js/jquery.tablesorter.js"></script>

    <!-- load roman numeral parser -->
    <script src="../js/parsers/parser-roman.js"></script>

# Javascript

# HTML