custom alphanumeric sort
custom alphanumeric sort
Hello All,
I need to implement a custom sort for an alphanumeric column.
The format of the data for this column is as follows:
-Two characters in length
-First character is a letter
-Second character is a number
So, for example vales would look something like this:
A1
A2
A3
B1
C3
H3
However, the tough requirement for this sort is that the Alpha aspect of the sort needs to be "asc", and the numeric aspect of the sort needs to be "desc", or vise versa.
For example, the expected result of sorted data would look like:
B3
B2
B1
or
B1
B2
B3
I know how to implement custom sort functions with the aoColumns option, so I am more looking for insight with the actual function itself.
Thanks!
I need to implement a custom sort for an alphanumeric column.
The format of the data for this column is as follows:
-Two characters in length
-First character is a letter
-Second character is a number
So, for example vales would look something like this:
A1
A2
A3
B1
C3
H3
However, the tough requirement for this sort is that the Alpha aspect of the sort needs to be "asc", and the numeric aspect of the sort needs to be "desc", or vise versa.
For example, the expected result of sorted data would look like:
B3
B2
B1
or
B1
B2
B3
I know how to implement custom sort functions with the aoColumns option, so I am more looking for insight with the actual function itself.
Thanks!
This discussion has been closed.
Replies
[code]Array.prototype.customSort=function(){
this.sort();
var lastChar = ''
var array = new Array();
var _array = new Array();
for (element in this){
if (this[element][0] !== lastChar){
if (_array.length > 0){
array.push(_array.reverse())
}
_array = new Array()
lastChar = this[element][0]
}
_array.push(this[element])
}
return array
};
var array = ['B2', 'A3', 'C1', 'C3', 'A1', 'B1', 'B3', 'A2'];
console.log('array = ' + array)
console.log('sortAsc = ' + array.customSort())[/code]
Yes, I just ran that in the firebug console and it generates that desired output.
I think that It needs to be modified to the correct format that custom datatable sort methods look for. For example:
http://datatables.net/examples/basic_init/multi_col_sort.html
I've not gotten round to adding custom sorts with datatables yet, so you'll have to have a blast at it yourself.
Glad I could help though
Chris
You were a great help. Thanks again!