简体   繁体   English

如何排序希腊字母/符号JavaScript

[英]How to sort greek letters/symbols JavaScript

I have managed to sort an array of objects by element name 'post_api_name' however some of these elements start with a Greek symbol (eg α-β Arteether). 我已经设法按元素名称'post_api_name'对对象数组进行排序,但是这些元素中的一些以希腊符号开头(例如α-βArteether)。 How can I convert these symbols to be placed alongside 0-9 and not Z+1. 如何将这些符号转换为0-9而不是Z + 1。

RESULT.sort(function(a, b) {
  var textA = a.post_api_name.toUpperCase();
  var textB = b.post_api_name.toUpperCase();
  return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});

For clarity, the current sorted results show as: 为清楚起见,当前排序的结果显示为:

abc α abcα

I am hoping to achieve: 我希望实现:

α abc αabc

You can try sorting using the Greek locale. 您可以尝试使用希腊语区域设置进行排序。 You would want to test with more cases as I have no idea what other sorting rules could apply. 您可能希望测试更多案例,因为我不知道可以应用哪些其他排序规则。 Browser support for localeCompare is still relatively cutting edge (IE11). 浏览器对localeCompare的支持仍然相对前沿(IE11)。 Maybe you can find a poly-fill . 也许你可以找到一个poly-fill

Maybe more preferably you might just have a list of Greek characters that you support and replace them with one of the characters between 9 and A for sorting purposes (:;<=>? or @). 也许更优选的是,您可能只有一个支持的希腊字符列表,并用9和A之间的字符之一替换它们以进行排序(:; <=>?或@)。

 var items = ['a', 'b', 'c', 'α', '9', '4', '1'] items.sort(function(a, b){ return a.localeCompare(b, 'el'); }); console.log(items); 

By default, the decimal code for the characters like 'α' will start from 128 ie they come under extended ASCII codes. 默认情况下,像'α'这样的字符的十进制代码将从128开始,即它们属于扩展的ASCII代码。

I know there is no particular way of doing this as per your requirement. 我知道根据您的要求,没有特别的方法可以做到这一点。 You can try the below function which will achieve you sorting the normal characters too and puts the greek/additional characters at first. 您可以尝试下面的功能,它将实现您对正常字符的排序,并首先放置希腊/附加字符。

var mySortedChars = [];

function sortMyArray(array) {
    array.forEach(function(val) {
       if (val.charCodeAt(0) > 127) {
          mySortedChars.push(val);
          _.pull(array, val);
       }
    });
    array.sort();
    array.forEach(function(val) {
       mySortedChars.push(val);
    });
    return mySortedChars;
}

The above function will return you the sorted characters as per your requirement. 上面的函数将根据您的要求返回已排序的字符。

Note : _.pull is a util from the lodash library. 注意_.pull是来自lodash库的util。 You may have to use your own methods to splice the special characters instead of using _.pull(array, val); 您可能必须使用自己的方法来拼接特殊字符,而不是使用_.pull(array, val);

A possible solution written in ES6. 用ES6编写的可能解决方案。 Split data out into standard and greek starting elements. 将数据拆分为标准和希腊起始元素。 Sort these results and then concat them. 对这些结果进行排序然后连接它们。

 const greekLower = new Set(); for (let c = 0x3B1; c < 0x3C9; c += 1) { greekLower.add(String.fromCodePoint(c)); } const data = ['xa', 'αb', 'yc', 'βd', 'ae', '1f', 'γg', '9h']; const standard = []; const greek = []; for (let v of data) { if (greekLower.has(v.charAt(0).toLowerCase())) { greek.push(v); } else { standard.push(v); } } standard.sort(); greek.sort(); const sorted = greek.concat(standard); document.getElementById('out').textContent = JSON.stringify(sorted); console.log(sorted); 
 <pre id="out"></pre> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM