gpt4 book ai didi

javascript - 如何按波斯字母表对列表进行排序?

转载 作者:行者123 更新时间:2023-11-29 18:41:46 26 4
gpt4 key购买 nike

我如何在 ul li 中读取 testArrray 并对这个列表进行排序?

lis.sort(function(a,b)) 支持英语/阿拉伯语/和字母表,不支持波斯语字母表。请帮帮我。谢谢

var alphabets = ["ا", "ب", "پ", "ت", "ث", "ج", "چ", "ح", "خ", "د", "ذ", "ر", "ز", "ژ", "س", "ش", "ص", "ض", "ط", "ظ", "ع", "غ", "ف", "ق",
"ک", "گ", "ل", "م", "ن", "و", "ه", "ی", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
];
var testArrray = ["ی", "گ", "ژ", "پ"];

var aChar;
var bChar;

function OrderFunc() {
testArrray.sort(function(a, b) {
return CharCompare(a, b, 0);
});
document.getElementById("result").innerHTML = testArrray;;
}

function CharCompare(a, b, index) {
if (index == a.length || index == b.length)
return 0;

aChar = alphabets.indexOf(a.toUpperCase().charAt(index));
bChar = alphabets.indexOf(b.toUpperCase().charAt(index));

if (aChar != bChar)
return aChar - bChar
else
return CharCompare(a, b, index + 1)
}
<html>
<head></head>
<body onload="OrderFunc()">
<div id="result"></div>
<ul class="myul">
<li>ی</li>
<li>پ</li>
<li>گ</li>
<li>ژ</li>
</ul>
</body>
</html>

最佳答案

String#localeCompare应该是语言环境感知的并适本地比较字符串,所以:

function OrderFunc() {
// Get the list
const ul = document.querySelector(".myul");
// Get its items as an array
const lis = [...ul.querySelectorAll("li")];
// Sort the array with localeCompare
lis.sort((a, b) => a.textContent.localeCompare(b.textContent));
// Move each of them to the end of the list; this
// puts them back in order
for (const li of lis) {
ul.appendChild(li);
}
}

实例:

function OrderFunc() {
// Get the list
const ul = document.querySelector(".myul");
// Get its items as an array
const lis = [...ul.querySelectorAll("li")];
// Sort the array with localeCompare
lis.sort((a, b) => a.textContent.localeCompare(b.textContent));
// Move each of them to the end of the list; this
// puts them back in order
for (const li of lis) {
ul.appendChild(li);
}
}
OrderFunc();
<div id="result"></div>
<ul class="myul">
<li>ی</li>
<li>پ</li>
<li>گ</li>
<li>ژ</li>
</ul>

您可能需要将一些选项传递给 localeCompare,请参阅 ECMAScript® Internationalization API Specification有关更多信息。


在您提出的评论中:

What will i do first Persian and Second English Words?

如果您要问如何在列表中将波斯语单词排在英语单词之上,我认为您可能必须检测文本是用什么脚本编写的。您应该是能够使用 JavaScript 正则表达式来做到这一点,但该功能(Unicode 属性 excapes)是新功能且尚未得到很好的支持。您可以使用 XRegExp library但是,要做到这一点:

// This checks to see if the FULL string is in Arabic script; you'll
// probably have to adjust it to fix your use case
const rexArabic = XRegExp("^\\p{Arabic}+$");
function OrderFunc() {
// Get the list
const ul = document.querySelector(".myul");
// Get its items as an array
const lis = [...ul.querySelectorAll("li")];
// Sort the array with localeCompare
lis.sort(({textContent: a}, {textContent: b}) => {
const aArabicScript = rexArabic.test(a);
const bArabicScript = rexArabic.test(b);
if (aArabicScript && !bArabicScript) {
// `a` is in Arabic script, `b` isn't; `a` should be first
return -1;
}
if (!aArabicScript && bArabicScript) {
// `b` is in Arabic script, `a` isn't; `b` should be first
return 1;
}
// They're in the same script
return a.localeCompare(b);
});
// Move each of them to the end of the list; this
// puts them back in order
for (const li of lis) {
ul.appendChild(li);
}
}

实例:

// This checks to see if the FULL string is in Arabic script; you'll
// probably have to adjust it to fix your use case
const rexArabic = XRegExp("^\\p{Arabic}+$");
function OrderFunc() {
// Get the list
const ul = document.querySelector(".myul");
// Get its items as an array
const lis = [...ul.querySelectorAll("li")];
// Sort the array with localeCompare
lis.sort(({textContent: a}, {textContent: b}) => {
const aArabicScript = rexArabic.test(a);
const bArabicScript = rexArabic.test(b);
if (aArabicScript && !bArabicScript) {
// `a` is in Arabic script, `b` isn't; `a` should be first
return -1;
}
if (!aArabicScript && bArabicScript) {
// `b` is in Arabic script, `a` isn't; `b` should be first
return 1;
}
// They're in the same script
return a.localeCompare(b);
});
// Move each of them to the end of the list; this
// puts them back in order
for (const li of lis) {
ul.appendChild(li);
}
}
OrderFunc();
<div id="result"></div>
<ul class="myul">
<li>ی</li>
<li>پ</li>
<li>Some English</li>
<li>گ</li>
<li>More English</li>
<li>ژ</li>
</ul>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/3.2.0/xregexp-all.min.js"></script>

关于javascript - 如何按波斯字母表对列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56474442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com