Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save avlima/6cbd559aa80acc32de9f07070abbd0b5 to your computer and use it in GitHub Desktop.
Save avlima/6cbd559aa80acc32de9f07070abbd0b5 to your computer and use it in GitHub Desktop.
how to sort records containing accents
<table id="example">
</table>
$(document).ready(function() {
var data = [
["Alberto Lima"],
["Dwarf"],
[ "Garrett Winters"],
["Álberto"],
[ "Hector"],
[ "Hígor" ],
[ "Wellington" ],
["Beto"]
];
$('#example').DataTable( {
"aaData": data,
"aoColumns": [
{
"sType": "portugues",
"title": "Name"
},
],
"aaSorting": [[ 0, "asc" ]],
} );
} );
jQuery.fn.dataTableExt.oSort["portugues-asc"] = function (a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}
jQuery.fn.dataTableExt.oSort["portugues-pre"] = function (data) {
var a = 'a';
var e = 'e';
var i = 'i';
var o = 'o';
var u = 'u';
var c = 'c';
var special_letters = {
"Á": a, "á": a, "Ã": a, "ã": a, "À": a, "à": a,
"É": e, "é": e, "Ê": e, "ê": e,
"Í": i, "í": i, "Î": i, "î": i,
"Ó": o, "ó": o, "Õ": o, "õ": o, "Ô": o, "ô": o,
"Ú": u, "ú": u, "Ü": u, "ü": u,
"ç": c, "Ç": c
};
for (var val in special_letters)
data = data.split(val).join(special_letters[val]).toLowerCase();
return data;
};
jQuery.fn.dataTableExt.oSort["portugues-asc"] = function (a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment