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)


 * Numerical sorts are crappy when numbers, signs and empty cells are present in a column. And for non-english numerical punctuation (languages where commas are used as a decimal separator) the situation is worst. There's no other way to sort such thins that what you had posted here. --Ciencia Al Poder (talk) -WikiDex 19:04, 21 April 2008 (UTC)

a useful workaround via a simple template
ref: http://rappelz.wikia.com/wiki/Template:Sortkey

The following template nicely works around this problem:

Usage
|CellContent
 * The leading | (pipe) character is the cell delimiter.

Inserts a hidden element into a cell to force a desired sort order for a specific column of a sortable.

Tips
To masquerade as a numerical sort the easiest thing to do is add a large constant (e.g. 10^n) to every value in the column then pad the result with leading zeros so that all sort keys use the same number of digits before a decimal point.

To masquerade as a sort by date the easiest thing to do is use
 * 1YYYYMMDD format for all dates that are A.D.
 * the leading 1 is significant but for convenience, it can be omitted if there are no B.C. dates in the column
 * 0yyyymmdd format for all dates that are B.C.
 * where yyyymmdd = 10^8 - YYYYMMDD
 * padding with a 0 is significant since alphabetical sort is used

Yes, it would be nice if the template performed either or both algorithms for the user. Najevi 04:52, 23 April 2008 (UTC)

Example
see http://rappelz.wikia.com/wiki/Template:Sortkey

Najevi 05:04, 23 April 2008 (UTC)