我正在努力使我的同类与国际角色合作。然而,当我尝试排序时,它被弄乱了,顺序不对。下面是我的代码:
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>
这是尝试排序时的输出:
dddd
cccc
čččč
ćććć
bbbb
aaaa
顺序应如下所示:
dddd
ćććć
čččč
cccc
bbbb
aaaa
有办法解决这个问题吗?我没有找到解决问题的办法,所以我试着自己写。
有人能帮我吗?
1条答案
按热度按时间i86rm4rw1#
你试过使用吗
Intl.Collator
具有reverse
? 它产生的订单与您的期望并不完全一致,但非常接近: