'How to sort list by Persian alphabet?
How can I read testArrray in ul li and sort this list?
lis.sort(function(a,b)) has English/Arabic/and alphabet support, not Persian alphabet support. Help me please. Thank you
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>
Solution 1:[1]
To sort a list alphabetically in Persion, I used this code with <meta charset="utf-8"> and its result was perfect:
const myList = ["?","?","?", "?"];
const collator = new Intl.Collator('fa');
const sortedLetters = myList.sort(collator.compare);
console.log(sortedLetters);
The Intl.Collator objects enable language sensitive string comparison.
For other languages, you can replace fa:
ar — Arabic
bg — Bulgarian
ca — Catalan
zh-Hans — Chinese, Han (Simplified variant)
cs — Czech
da — Danish
de — German
el — Modern Greek (1453 and later)
en — English
es — Spanish
fi — Finnish ,....
for more information please visit https://dev.to/aumayeung/comparing-non-english-strings-with-javascript-collators-57bf and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator
Solution 2:[2]
I never tested it and really cannot do it because I have no idea what letter comes before or after in arabic but I can point you to the right direction. You will need to use localeCompare in a way similiar to this:
arr.sort((x,y)=>x.localeCompare(y, 'ar-ma'))
ar-ma being Arabian Morocco language code which you should change to the language code of your needs.
Solution 3:[3]
I had same problem And I wrote this function , It works well for me .. so I decided to share it here for you :
// costome order of english and persian letters
var persianAlphabets = [" ", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
// function for sorting an array of persian words
function customSort(x, y) {
var i = 0;
var maxLen = Math.min(x.length, y.length);
function increseIndex() {
if (persianAlphabets.indexOf(x[i]) == persianAlphabets.indexOf(y[i]) &&
i <= maxLen) {
i++;
increseIndex();
} else return
}
increseIndex();
if (persianAlphabets.indexOf(x[i]) <
persianAlphabets.indexOf(y[i])) {
return -1;
}
if (persianAlphabets.indexOf(x[i]) >
persianAlphabets.indexOf(y[i])) {
return 1;
}
return 0;
}
// sample array for testing
var testArrray = [
"????",
"??????",
"??",
"???? ???",
"??? ?????",
"??? ?????",
"????",
"???? 1",
"???????",
"? ?",
"?????",
"??????",
"???? 2",
"????"
];
// applying the sorting function
testArrray.sort(customSort);
// testing resoults
console.log(testArrray);
Solution 4:[4]
Crowder gave the correct answer. But if you want to take matter in your own hands, you can apply your custom sorting function which sorts by comparing indexes in the reference array.
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 = ["?","?","?","?"];
testArrray.sort(customSort(alphabets));
function customSort(alphabets){
return function (x,y) {
if (alphabets.indexOf(x) < alphabets.indexOf(y)) {
return -1;
}
if (alphabets.indexOf(x) > alphabets.indexOf(y)) {
return 1;
}
return 0;
}
}
console.log(testArrray);
Solution 5:[5]
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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nasim B. D |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | shreyas d |
| Solution 5 | Muhammad Usman Rajpotzada |
