使用 localeCompare 对标题进行排序
Sort titles with localCompare
使用 Eleventy 作为静态站点生成器我无法弄清楚如何按特定字母表(在我的例子中是拉脱维亚语,lv)对标题进行排序。 Documentation related sorting
到目前为止,下面的脚本适用于英语。
eleventyConfig.addCollection("postsDescending", (collection) =>
collection.getFilteredByGlob("src/posts/*.md").sort((a, b) => {
if (a.data.title > b.data.title) return 1;
else if (a.data.title < b.data.title) return -1;
else return 0;
})
);
在我看来,我尝试了 localeCompare,但出现错误 collection.getFilteredByGlob(...).from is not a function
const alphabet = ['a','ā','b','c','č','d','e','ē','f','g','ģ','h','i','ī','j','k','ķ','l','ļ','m','n','ņ','o','p','r','s','š','t','u','ū','v','z','ž'];
eleventyConfig.addCollection("postsDescending", function(collection) {
return collection.getFilteredByGlob("src/posts/*.md").from(alphabet).sort(function(a, b) {
return a.localeCompare(b, 'lv', { sensitivity: 'base' });
});
});
不用说我是 Javascript 的初学者...非常感谢任何帮助!
原来我自己能解决这个问题
eleventyConfig.addCollection("postsDescending", (collection) =>
collection.getFilteredByGlob("src/posts/*.md").sort((a, b) => {
return a.data.title.localeCompare(b.data.title, 'lv', { sensitivity: 'base' });
})
);
使用 Eleventy 作为静态站点生成器我无法弄清楚如何按特定字母表(在我的例子中是拉脱维亚语,lv)对标题进行排序。 Documentation related sorting
到目前为止,下面的脚本适用于英语。
eleventyConfig.addCollection("postsDescending", (collection) =>
collection.getFilteredByGlob("src/posts/*.md").sort((a, b) => {
if (a.data.title > b.data.title) return 1;
else if (a.data.title < b.data.title) return -1;
else return 0;
})
);
在我看来,我尝试了 localeCompare,但出现错误 collection.getFilteredByGlob(...).from is not a function
const alphabet = ['a','ā','b','c','č','d','e','ē','f','g','ģ','h','i','ī','j','k','ķ','l','ļ','m','n','ņ','o','p','r','s','š','t','u','ū','v','z','ž'];
eleventyConfig.addCollection("postsDescending", function(collection) {
return collection.getFilteredByGlob("src/posts/*.md").from(alphabet).sort(function(a, b) {
return a.localeCompare(b, 'lv', { sensitivity: 'base' });
});
});
不用说我是 Javascript 的初学者...非常感谢任何帮助!
原来我自己能解决这个问题
eleventyConfig.addCollection("postsDescending", (collection) =>
collection.getFilteredByGlob("src/posts/*.md").sort((a, b) => {
return a.data.title.localeCompare(b.data.title, 'lv', { sensitivity: 'base' });
})
);