I am trying to make my sort working with international characters. However is messed up when I try to sort, the order is not right. Below is my code:
function character_substitute(string) {
var first_char = string.replace( /<.*?>/g, "" ).toLowerCase().charAt(0);
var chars = /[šđžčć]/g;
if (first_char.match(chars)) {
if (first_char == "š") { first_char = first_char.replace("š", "s"); return first_char; }
if (first_char == "ž") { first_char = first_char.replace("ž", "z"); return first_char; }
if (first_char == "č") { first_char = first_char.replace("č", "c"); return first_char; }
if (first_char == "ć") { first_char = first_char.replace("ć", "c"); return first_char; }
if (first_char == "đ") { first_char = first_char.replace("đ", "d"); return first_char; }
}
return first_char;
}
jQuery.fn.dataTableExt.oSort['balkan_string-asc'] = function(a,b) {
x = character_substitute(a);
y = character_substitute(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['balkan_string-desc'] = function(a,b) {
x = character_substitute(a);
y = character_substitute(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
$(function() {
$("#example").DataTable({
columns: [{
type: 'balkan_string',
targets: 0
}]
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css" rel="stylesheet" >
<table id="example">
<thead>
<tr>
<th>String</th>
</tr>
</thead>
<tbody>
<tr>
<td>aaaa</td>
</tr>
<tr>
<td>bbbb</td>
</tr>
<tr>
<td>cccc</td>
</tr>
<tr>
<td>čččč</td>
</tr>
<tr>
<td>ćććć</td>
</tr>
<tr>
<td>dddd</td>
</tr>
</tbody>
</table>
This is an output when you try to sort:
dddd
cccc
čččč
ćććć
bbbb
aaaa
The order should be like this:
dddd
ćććć
čččč
cccc
bbbb
aaaa
Is there a way to solve this? I didn't found the solution to my problem, so I try to write on my own.
Can anybody try to help me with this?