Javascript 盖茨比图像查询返回无序图像
我正在制作一个盖茨比图像库,包含650幅图像。Javascript 盖茨比图像查询返回无序图像,javascript,bash,sorting,graphql,Javascript,Bash,Sorting,Graphql,我正在制作一个盖茨比图像库,包含650幅图像。 图像按顺序命名:1.jpg、2.jpg、3.jpg、…、650.jpg 然后我运行此查询,该查询应按名称排序: export const query = graphql` query ImagesForGallery { images: allFile(filter: { relativeDirectory: { eq: "gallery" } }, sort: { fields: name }) { e
图像按顺序命名:
1.jpg、2.jpg、3.jpg、…、650.jpg
然后我运行此查询,该查询应按名称排序:
export const query = graphql`
query ImagesForGallery {
images: allFile(filter: { relativeDirectory: { eq: "gallery" } }, sort: { fields: name }) {
edges {
node {
childImageSharp {
thumb: fluid(maxWidth: 270, maxHeight: 270) {
...GatsbyImageSharpFluid
originalName
}
full: fluid(maxWidth: 1024) {
...GatsbyImageSharpFluid
}
}
}
}
}
}
`;
然后,图片按以下顺序返回:1.jpg,10.jpg,100.jpg,101.jpg,…,2.jpg,20.jpg,200.jpg,201.jpg,…
因此,图像不会以数字顺序返回,而是使用某种伪字母顺序(
我如何以数字顺序返回它们,或者,如何重命名所有图像以符合盖茨比顺序
编辑:最简单的测试方法是使用6张名为:1.jpg,2.jpg,10.jpg,11.jpg,100.jpg,101.jpg
我认为你不能直接在盖茨比查询中完成,因为它有点特殊
你能做的就是在你得到数据后用javascript处理它。我已经编写了一些代码来处理这个任务
var points = ['1.jpg', '32.jpg', '210.jpg', '11.jpg', '1020.jpg', '101.jpg'];
points.sort((a, b) => split(a) - split(b));
const split = (strNumber) => strNumber.split('.')[0]
// will give
["1.jpg", "11.jpg", "32.jpg", "101.jpg", "210.jpg", "1020.jpg"]
让我知道它是否有效这是我已经做过的,但我很恼火我必须这么做。在Windows、Mac和Ubuntu文件浏览器中,当你按名称对文件进行排序时,这是正确的,但是当graphql查询执行时,它以某种方式与所有主要操作系统平台不同…也许我会打开一个Github问题。谢谢你的帮助啊!啊,很抱歉听到这个消息,我已经有一段时间没有联系盖茨比了,但是你有没有尝试在你的查询中使用排序/筛选/格式?