Forum:Problem with numerical sort in sortable

I ran into a problem with a column of mixed positive and negative integers in a sortable.

Eventually I used an alphabetical search and hidden sort keys however, I'd like to understand what is going wrong with the yellow highlighted column in the table below.

In case some one else runs into the same problem here is the fix I used for expedience.

It is not a pretty fix but a workaround for now:- Added a hidden alphabetical sort key to each element of the problem column.

hiding was done using 105

and trial and error leads me to the conclusion that when using a hidden key it is always treated as a string and so is sorted alphabetically not numerically. For this reason the keys (105, 95, 80, 5) will not work but (105, 095, 080, 005) will work. The padding with 0's is necessary for an alphabetical sort to properly masquerade as a numerical sort.

The code and table below demonstrate the problem and the workaround.


 * The fourth column is the original problem column. I now believe that it is being sorted alphabetically perhaps due to there being a mixture of +ve and -ve numbers.
 * The fifth column has 100 added to all numbers form the fourth column to force all +ve numbers. I believe that this column is being sorted in numerical order.
 * First and Fifth columns sort correctly.
 * The first three columns use hidden sort keys and I now believe they are being sorted alphabetically perhaps due to a  element being used to hide the sort key. Refer to wikipedia help on this subject.


 * {|class="sortable" border="1" cellpadding="1" cellspacing="0" style="border-collapse:collapse;"

!align="left" width="110px" BGCOLOR="#00efef"|Hidden 105,095,..,045 !align="left" width="110px" BGCOLOR="#efefef"|Hidden 105,95,..,45 !align="left" width="110px" BGCOLOR="#efefef"|Hidden +5,-5,..,-55 !align="left" width="110px" BGCOLOR="#efef00"|Problem (mixed sign) !align="left" width="110px" BGCOLOR="#00efef"|Numerical 105,95,..,45
 * 105 +5
 * 105 +5
 * +5
 * +5
 * 105
 * 095 -5
 * 95 -5
 * -5
 * -5
 * 95
 * 095 -5
 * 95 -5
 * -5
 * -5
 * 95
 * 090 -10
 * 90 -10
 * -10
 * -10
 * 90
 * 090 -10
 * 90 -10
 * -10
 * -10
 * 90
 * 080 -20
 * 80 -20
 * -20
 * -20
 * 80
 * 080 -20
 * 80 -20
 * -20
 * -20
 * 80
 * 080 -20
 * 80 -20
 * -20
 * -20
 * 80
 * 075 -25
 * 75 -25
 * -25
 * -25
 * 75
 * 070 -30
 * 70 -30
 * -30
 * -30
 * 70
 * 070 -30
 * 70 -30
 * -30
 * -30
 * 70
 * 045 -55
 * 45 -55
 * -55
 * -55
 * 45
 * }
 * 070 -30
 * 70 -30
 * -30
 * -30
 * 70
 * 045 -55
 * 45 -55
 * -55
 * -55
 * 45
 * }
 * }
 * }

Thank you for considering this matter.

Najevi 15:57, 20 April 2008 (UTC)