For columns in y-dimension how to do natural sort for alpha numeric column names ?
For example: consider column names AA1, AA2, AA3, AA10, AA11. These are listed in order AA1, AA10, AA11, AA2, AA3 in pivot table y-dimension.
Desired order of columns is AA1, AA2, AA3, AA10, AA11
Free jqGrid 4.9 contains full rewritten version of
jqPivot
. I tried to hold compatibility with the previous version, but it contains many advanced features. I tried to describe there in wiki.Not so many people uses
jqPivot
. So I remind what it do. It gets an input data as source and generate new data, which will be input data for jqGrid. AdditionallyjqPivot
generatescolModel
based on input data andyDimension
parameter. During analyzing of input datajqPivot
sorts input data byxDimension
and byyDimension
. The order or sorting ofxDimension
defines the order of rows of resulting grid. The order or sorting ofyDimension
defines the order of columns of resulting grid and the total number of resulting columns. The optionscompareVectorsByX
andcompareVectorsByY
of allows to specify callback function which will be used for custom sorting by the whole x or y vector. It's important to understand that sorting function not only specify the the order of columns, but it informsjqPivot
which vectors should be interpreted as the same. For example it can interpret the values12
,12.0
and012.00
as the same and specify that12.0
is larger as6
.I describe below some ways which can be used to customize sorting by
xDimension
andyDimension
.First of all one can specify
skipSortByX: true
orskipSortByY: true
parameters. In the case the input data have to be already sorted in the order which you want. The next important options are Boolean optionscaseSensitive
(with default valuefalse
) andtrimByCollect
(default valuetrue
).caseSensitive: true
can be used to distinguish input data by case andtrimByCollect: false
can be used to hold trailing spaces in the input data.Some other important option can be specified in
xDimension
oryDimension
:sorttype
andsortorder
.sortorder: "desc"
can be used to reverse the order of sorted data. The optionsorttype
can be"integer"
(or"int"
) which means to truncate (Math.floor(Number(inputValue))
) input data during sorting; The values"number"
,"currency"
and"float"
means that the input data should be converted to numbers during sorting (Number(inputValue)
). Finally one can don't specify anysorttype
, but specifycompare
callback function instead. Thecompare
callback is function with two parameters and it should return well known -1, 0 or 1 values.For example I created the demo for one issue. One asked my about the following situation. The web site contains login, which identifies the country of the user. One want to set the user's country as the first in the sorting order. The demo uses the following
yDimension
parameter:It sets
"Germany"
first in the sorting order. As the results one sees the results like on the picture belowYou can use the same approach using the code for natural compare from the answer and you will implements your requirements.
In more advanced cases one can use the options
compareVectorsByX
andcompareVectorsByY
. The requirement was to place specific country only in one specific year on the first place holding the standard order on all other cases. The corresponding demo usescompareVectorsByY
to implement the requirement. It displaysand uses the following
compareVectorsByY
:It's important to mention that
compareVectorsByY
callback function should return object with two properties:index
andresult
. The value ofresult
property should be -1, 0 or 1. The value ofindex
property should be -1 in case ofresult: 0
and be 0-based index of vectors wherevector1
andvector2
are different.