Discussion:
Is there have any JQuery plugin that can sort 1A,2A,3A,... 10A, 11A, 12A, 13A, 14A?
HenryRock
2010-02-02 15:37:13 UTC
Permalink
I facing a problem in sorting :

1A,2A,3A,... 10A, 11A, 12A, 13A, 14A

Sample data.

I using the SQL to sort ASC but it return result as below:

12A
13A
14A
10A
11A
1A
2A
3A
4A
5A
6A
7A
8A
9A

May I know is there have any plugin that can sort the sample data
above to 1A until 14A?

Thanks...
aquaone
2010-02-02 16:43:39 UTC
Permalink
Sort as in a tablesorter parser, an array, or what?
Post by HenryRock
1A,2A,3A,... 10A, 11A, 12A, 13A, 14A
Sample data.
12A
13A
14A
10A
11A
1A
2A
3A
4A
5A
6A
7A
8A
9A
May I know is there have any plugin that can sort the sample data
above to 1A until 14A?
Thanks...
Randall Morgan
2010-02-02 17:18:04 UTC
Permalink
Hi,

I'm not sure what DBMS you are using but most can sort many ways. You
may want to read your DBMS manuals and look for a natural sort method.
MySQL uses something like ORDER BY <column_name> NATURAL ASC or ORDER
BY <column_name> NATURAL DESC. Somewhere on the net should be an old
thread about sorting mixed numbers and alpha characters in MySQL. Try
Googling the topic. If the DB can handle the sort for you, you'll save
time and network traffic. Unless you really need to be able to sort
this on the client side.

Hope this helps

Randy
Post by HenryRock
1A,2A,3A,... 10A, 11A, 12A, 13A, 14A
Sample data.
12A
13A
14A
10A
11A
1A
2A
3A
4A
5A
6A
7A
8A
9A
May I know is there have any plugin that can sort the sample data
above to 1A until 14A?
Thanks...
--
If you ask me if it can be done. The answer is YES, it can always be
done. The correct questions however are... What will it cost, and how
long will it take?
JamaicaMan
2010-02-02 17:49:04 UTC
Permalink
See this link:
http://db4free.blogspot.com/2006/06/sorting-of-numeric-values-mixed-with.html
Scott Sauyet
2010-02-03 16:33:44 UTC
Permalink
Post by HenryRock
1A,2A,3A,... 10A, 11A, 12A, 13A, 14A
[ ... ]
May I know is there have any plugin that can sort the sample data
above to 1A until 14A?
Note that it's not hard to do this in Javascript, though:

var sorter = (function() {
var regex = /^(\d+)([a-z]+)$/i;
return function(o1, o2) {
var left = regex.exec("" + o1);
var right = regex.exec("" + o2);
if (left) left[1] = Number(left[1]);
if (right) right[1] = Number(right[1]);
if (!left) return (right ? 1 : (
"" + o1 < "" + o2 ? -1 : ("" + o1 > "" + o2 ? 1 : 0))
);
if (!right) return -1
if (left[1] < right[1]) {
return -1;
} else if (left[1] > right[1]) {
return 1;
} else {
if (left[2] < right[2]) {
return -1;
} else if (left[2] > right[2]) {
return 1;
} else {
return 0;
}
}
};
})();

var values = ["12A", "11B", "1A", "2C", "2B", "2A",
"1a", "4B", "13C", "12C", "3B", "3A"];
// 1A,1a,2A,2B,2C,3A,3B,4B,11B,12A,12C,13C
alert(values.sort(sorter));

Good luck,

-- Scott

Continue reading on narkive:
Loading...