/[js_locale]/trunk/sort.html
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/sort.html

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Sun Oct 24 19:16:22 2004 UTC (19 years, 8 months ago) by dpavlin
File MIME type: text/html
File size: 2616 byte(s)
first import of JavaScript Locale support.

Right now, it's hr_HR.ISO-8859-2, but different locales can be created using
included perl script from local machine settings.

Tested on Firefox 1.0 PR and Internet Explorer 6.0


1 <html>
2 <head>
3 <title>JavaScript sort which honors static locale settings</title>
4 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
5 </head>
6 <body bgcolor="white">
7
8 <p>
9 This is a demo of JavaScript sort which honors static locale LC_COLLATE
10 settings, embedded in one JavaScript variable which can be created from
11 local system using attached perl script.
12 </p><p>
13 Script by Dobrica Pavlinu¹iæ, &lt;dpavlin@rot13.org&gt; 2004-10-24
14 </p>
15
16 <tt>
17
18 <script type="text/javascript">
19
20 // locale in which to sort (basically a alphabet in correct dictionary sort order)
21 var _lc_locale = '_0123456789aAáÁãÃâÂäı¡bBcCçÇèÈæÆdDïÏðÐeEéÉìÌëËêÊfFgGhHiIíÍîÎjJkKlLåŵ¥³£mMnNñÑòÒoOóÓôÔöÖõÕpPqQrRàÀøØsS¶¦ºªß¹©tT»«þÞuUúÚùÙüÜûÛvVwWxXyYýÝzZ¼¬¿¯¾®';
22
23 // produce equivavlent of alphabet in native JavaScript sort order
24 var _lc_native = _lc_locale.split("").sort().join("");
25
26 function lc_debug(msg) {
27 // comment out to disable debug
28 //return;
29 document.write('<div style="color: gray; font-size: small;">'+msg+'</div>');
30 }
31
32 // create character remapping array
33 var _lc_l2n_arr = new Array();
34 var r = 0;
35 for (var i=0; i < _lc_locale.length; i++) {
36 if (_lc_locale.charAt(i) != _lc_native.charAt(i)) {
37 _lc_l2n_arr[_lc_locale.charAt(i)] = _lc_native.charAt(i);
38 r++;
39 }
40 }
41
42 lc_debug(
43 "_lc_native:"+_lc_native+"<br>"+
44 "_lc_locale:"+_lc_locale+"<br>"+
45 "remapped "+r+" characters from table of "+_lc_locale.length+"/"+_lc_native.length+" locale/native characters<br>"
46 );
47
48 // comment out following line to disable caching of locale terms
49 var _lc_cache = new Array();
50
51 // convert string to correct sort order according to locale
52 function _lc(str) {
53 if (_lc_cache && _lc_cache[str]) {
54 return _lc_cache[str];
55 } else {
56 var out = '';
57 for (var i=0; i <= str.length; i++) {
58 var c = str.charAt(i);
59 if (_lc_l2n_arr[c]) {
60 out += _lc_l2n_arr[c];
61 } else {
62 out += c;
63 }
64 }
65 if (_lc_cache) _lc_cache[str] = out;
66 return out;
67 }
68 }
69
70 // sort function with locale support
71 function _lc_sort(a,b) {
72 var a_l = _lc(a);
73 var b_l = _lc(b);
74
75 lc_debug(a+' '+( a_l < b_l ? '<' : ( a_l == b_l ? '==' : '>' ) )+' '+b+' [ '+a_l+' '+b_l+' ]');
76
77 if (a_l < b_l) {
78 return -1;
79 } else if (a_l == b_l) {
80 return 0;
81 } else {
82 return 1;
83 }
84 }
85
86 // test string array of Croatian words
87 var test = [
88 'abrakadabra',
89 'èevap',
90 'ðak',
91 'd¾amija',
92 'æuk',
93 'pero',
94 '¾aba',
95 'Ðakovo',
96 'Zagreb'
97 ];
98
99 // create some output
100 document.write(
101 "<br>original: ",test.join(" * "),
102 "<br>native: ",test.sort().join(" * "),
103 "<br>locale: ",test.sort(_lc_sort).join(" * ")
104 );
105
106 </script>
107
108 </tt>
109
110 <br>
111 <p>Sorting finished.</p>
112
113 </body>
114 </html>

  ViewVC Help
Powered by ViewVC 1.1.26