问题描述
我有一个表格,其中包含一组混合文本和数字的数据。我正在使用jQuery和对其进行排序。无法正确排序的数据是设备标签,例如AHU-1,AHU-2,AHU-10。问题是,给定这些示例值,AHU-10将被放置在AHU-1和AHU-2之间。我发现强制'数字'排序并不能解决问题。
I have a table with a column of data that is mixed text and numbers. I'm sorting it using jQuery and the tablesorter plugin. The data that won't sort correctly is equipment tags, for example, "AHU-1", "AHU-2", "AHU-10". The problem is, given those example values, AHU-10 will be placed between AHU-1 and AHU-2. I've found forcing a 'digit' sort doesn't solve the problem.
这是我的问题:1)有没有人知道我可以使用的现有解析器这个情况?如果没有,那么我需要编写自己的解析器,在这种情况下2)我应该如何编写解析器?我应该尝试将每个字母翻译成一个数字并进行数字排序吗?这是我最初的想法。
Here's my question: 1) Does anyone know of an existing parser that I can use in this situation? If there isn't one then I'll need to write my own parser, in which case 2) How should I write the parser? Should I try and translate every letter to a number and do a numeric sort? That's my initial thought.
还有一件事,我不确定连字符是否是分隔符。 AHU-1也可以是AHU1,或AHU 1,或AHU:1,或其他东西。
One more thing, I don't know for sure that a hyphen will be the delimiter. "AHU-1" could also be "AHU1", or "AHU 1", or "AHU:1", or something else.
推荐答案
您需要编写自己的解析器。您正在寻找的是。有很多javascript自然排序算法。我找不到一个为tablesorter插件预先写好的内容,但google搜索出现了不少。
You do need to write your own parser. What you are looking for is called "natural sort". There are plenty of javascript natural sort algorithms out there. I couldn't find one prewritten for the tablesorter plugin, but googling turns up quite a few.
这篇关于jQuery tablesorter - 使用混合文本和数字对列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!