{"id":13591477,"url":"https://github.com/kylefox/jquery-tablesort","last_synced_at":"2025-04-04T05:08:34.659Z","repository":{"id":2656599,"uuid":"3647338","full_name":"kylefox/jquery-tablesort","owner":"kylefox","description":"A tiny \u0026 dead-simple jQuery plugin for sortable tables.","archived":false,"fork":false,"pushed_at":"2022-01-14T13:52:41.000Z","size":94,"stargazers_count":258,"open_issues_count":14,"forks_count":96,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-03-28T04:14:41.350Z","etag":null,"topics":["javascript","jquery","jquery-plugin","sort","tablesorter"],"latest_commit_sha":null,"homepage":"http://dl.dropbox.com/u/780754/tablesort/index.html","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kylefox.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-03-07T08:45:30.000Z","updated_at":"2025-02-20T11:13:08.000Z","dependencies_parsed_at":"2022-07-18T22:01:17.980Z","dependency_job_id":null,"html_url":"https://github.com/kylefox/jquery-tablesort","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylefox%2Fjquery-tablesort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylefox%2Fjquery-tablesort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylefox%2Fjquery-tablesort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylefox%2Fjquery-tablesort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kylefox","download_url":"https://codeload.github.com/kylefox/jquery-tablesort/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247123107,"owners_count":20887261,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","jquery","jquery-plugin","sort","tablesorter"],"created_at":"2024-08-01T16:00:58.083Z","updated_at":"2025-04-04T05:08:34.643Z","avatar_url":"https://github.com/kylefox.png","language":"HTML","readme":"A tiny \u0026 dead-simple jQuery plugin for sortable tables. Here's a basic [demo](http://dl.dropbox.com/u/780754/tablesort/index.html).\n\nMaintainers Wanted\n---\n\n![](https://img.shields.io/badge/maintainers-wanted-red.svg)\n\nI don't use this library much anymore and don't have time to maintain it solo.\n\nIf you are interested in helping me maintain this library, please let me know! [**Read more here \u0026raquo;**](https://github.com/kylefox/jquery-tablesort/issues/32)\n\nYour help would be greatly appreciated!\n\nInstall\n---\n\nJust add jQuery \u0026 the tablesort plugin to your page:\n\n```html\n\u003cscript src=\"http://code.jquery.com/jquery-latest.min.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"jquery.tablesort.js\"\u003e\u003c/script\u003e\n```\n\n(The plugin is also compatible with [Zepto.js](https://github.com/madrobby/zepto)).\n\nIt's also available via [npm](https://www.npmjs.com/package/jquery-tablesort)\n\n`npm install jquery-tablesort`\n\nand [bower](https://bower.io/)\n\n`bower install jquery-tablesort`\n\nBasic use\n---\n\nCall the appropriate method on the table you want to make sortable:\n\n```javascript\n$('table').tablesort();\n```\n\nThe table will be sorted when the column headers are clicked.\n\nTo prevent a column from being sortable, just add the `no-sort` class:\n\n```html\n\u003cth class=\"no-sort\"\u003ePhoto\u003c/th\u003e\n```\n\nYour table should follow this general format:\n\n\u003e Note: If you have access to the table markup, it's better to wrap your table rows\nin `\u003cthead\u003e` and `\u003ctbody\u003e` elements (see below), resulting in a slightly faster sort.\n\u003e\n\u003e If you can't use `\u003cthead\u003e`, the plugin will fall back by sorting all `\u003ctr\u003e` rows\nthat contain a `\u003ctd\u003e` element using jQuery's `.has()` method (ie, the header row,\ncontaining `\u003cth\u003e` elements, will remain at the top where it belongs).\n\n```html\n\u003ctable\u003e\n\t\u003cthead\u003e\n\t\t\u003ctr\u003e\n\t\t\t\u003cth\u003e\u003c/th\u003e\n\t\t\t...\n\t\t\u003c/tr\u003e\n\t\u003c/thead\u003e\n\t\u003ctbody\u003e\n\t\t\u003ctr\u003e\n\t\t\t\u003ctd\u003e\u003c/td\u003e\n\t\t\t...\n\t\t\u003c/tr\u003e\n\t\u003c/tbody\u003e\n\u003c/table\u003e\n```\n\nIf you want some imageless arrows to indicate the sort, just add this to your CSS:\n\n```css\nth.sorted.ascending:after {\n\tcontent: \"  \\2191\";\n}\n\nth.sorted.descending:after {\n\tcontent: \" \\2193\";\n}\n```\n\nHow cells are sorted\n---\n\nAt the moment cells are naively sorted using string comparison. By default, the `\u003ctd\u003e`'s text is used, but you can easily override that by adding a `data-sort-value` attribute to the cell. For example to sort by a date while keeping the cell contents human-friendly, just add the timestamp as the `data-sort-value`:\n\n```html\n\u003ctd data-sort-value=\"1331110651437\"\u003eMarch 7, 2012\u003c/td\u003e\n```\n\nThis allows you to sort your cells using your own criteria without having to write a custom sort function. It also keeps the plugin lightweight by not having to guess \u0026 parse dates.\n\nDefining custom sort functions\n---\n\nIf you have special requirements (or don't want to clutter your markup like the above example) you can easily hook in your own function that determines the sort value for a given cell.\n\nCustom sort functions are attached to `\u003cth\u003e` elements using `data()` and are used to determine the sort value for all cells in that column:\n\n```javascript\n// Sort by dates in YYYY-MM-DD format\n$('thead th.date').data('sortBy', function(th, td, tablesort) {\n\treturn new Date(td.text());\n});\n\n// Sort hex values, ie: \"FF0066\":\n$('thead th.hex').data('sortBy', function(th, td, tablesort) {\n\treturn parseInt(td.text(), 16);\n});\n\n// Sort by an arbitrary object, ie: a Backbone model:\n$('thead th.personID').data('sortBy', function(th, td, tablesort) {\n\treturn App.People.get(td.text());\n});\n```\n\nSort functions are passed three parameters:\n\n* the `\u003cth\u003e` being sorted on\n* the `\u003ctd\u003e` for which the current sort value is required\n* the `tablesort` instance\n\nCustom comparison functions\n---\n\nIf you need to implement more advanced sorting logic, you can specify a comparison function with the `compare` setting. The function works the same way as the `compareFunction` accepted by [`Array.prototype.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort):\n\n```javascript\nfunction compare(a, b) {\n  if (a \u003c b) {\n    return -1;\t\t// `a` is less than `b` by some ordering criterion\n  }\n  if (a \u003e b) {\n    return 1;\t\t\t// `a` is greater than `b` by the ordering criterion\n  }\n\n  return 0;\t\t\t\t// `a` is equal to `b`\n}\n```\n\nEvents\n---\n\nThe following events are triggered on the `\u003ctable\u003e` element being sorted, `'tablesort:start'` and `'tablesort:complete'`. The `event` and `tablesort` instance are passed as parameters:\n\n```javascript\n$('table').on('tablesort:start', function(event, tablesort) {\n\tconsole.log(\"Starting the sort...\");\n});\n\n$('table').on('tablesort:complete', function(event, tablesort) {\n\tconsole.log(\"Sort finished!\");\n});\n```\n\ntablesort instances\n---\n\nA table's tablesort instance can be retrieved by querying the data object:\n\n```javascript\n$('table').tablesort(); \t\t\t\t\t\t\t\t\t\t\t\t// Make the table sortable.\nvar tablesort = $('table').data('tablesort'); \t// Get a reference to it's tablesort instance\n```\n\nProperties:\n\n```javascript\ntablesort.$table \t\t\t// The \u003ctable\u003e being sorted.\ntablesort.$th\t\t\t\t\t// The \u003cth\u003e currently sorted by (null if unsorted).\ntablesort.index\t\t\t\t// The column index of tablesort.$th (or null).\ntablesort.direction\t\t// The direction of the current sort, either 'asc' or 'desc' (or null if unsorted).\ntablesort.settings\t\t// Settings for this instance (see below).\n```\n\nMethods:\n\n```javascript\n// Sorts by the specified column and, optionally, direction ('asc' or 'desc').\n// If direction is omitted, the reverse of the current direction is used.\ntablesort.sort(th, direction);\n\ntablesort.destroy();\n```\n\nDefault Sorting\n---\n\nIt's possible to apply a default sort on page load using the `.sort()` method described above. Simply grab the tablesort instance and call `.sort()`, padding in the `\u003cth\u003e` element you want to sort by.\n\nAssuming your markup is `\u003ctable class=\"sortable\"\u003e` and the column to sort by default is `\u003cth class=\"default-sort\"\u003e` you would write:\n\n```javascript\n$(function() {\n    $('table.sortable').tablesort().data('tablesort').sort($(\"th.default-sort\"));\n});\n```\n\nSettings\n---\n\nHere are the supported options and their default values:\n\n```javascript\n$.tablesort.defaults = {\n\tdebug: $.tablesort.DEBUG,\t\t// Outputs some basic debug info when true.\n\tasc: 'sorted ascending',\t\t// CSS classes added to `\u003cth\u003e` elements on sort.\n\tdesc: 'sorted descending',\n\tcompare: function(a, b) {\t\t// Function used to compare values when sorting.\n\t\tif (a \u003e b) {\n\t\t\treturn 1;\n\t\t} else if (a \u003c b) {\n\t\t\treturn -1;\n\t\t} else {\n\t\t\treturn 0;\n\t\t}\n\t}\n};\n```\n\nYou can also change the global debug value which overrides the instance's settings:\n\n```javascript\n$.tablesort.DEBUG = false;\n```\n\nAlternatives\n---\n\nI don't use this plugin much any more — most of the fixes \u0026 improvements are provided by contributors.\n\nIf this plugin isn't meeting your needs and you don't want to submit a pull-request, here are some alternative table-sorting plugins.\n\n* [Stupid jQuery Table Sort](https://github.com/joequery/Stupid-Table-Plugin)\n\n_(Feel free to suggest more by [opening a new issue](https://github.com/kylefox/jquery-tablesort/issues/new))_\n\nContributing\n---\n\nAs always, all suggestions, bug reports/fixes, and improvements are welcome.\n\nMinify JavaScript with [Closure Compiler](http://closure-compiler.appspot.com/home) (default options)\n\nHelp with any of the following is particularly appreciated:\n\n* Performance improvements\n* Making the code as concise/efficient as possible\n* Browser compatibility\n\nPlease fork and send pull requests, or [report an issue.](https://github.com/kylefox/jquery-tablesort/issues)\n\n# License\n\njQuery tablesort is distributed under the MIT License.\nLearn more at http://opensource.org/licenses/mit-license.php\n\nCopyright (c) 2012 Kyle Fox\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\nLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","funding_links":[],"categories":["Table of contents"],"sub_categories":["Data Table"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylefox%2Fjquery-tablesort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkylefox%2Fjquery-tablesort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylefox%2Fjquery-tablesort/lists"}