custom alphanumeric sort

custom alphanumeric sort

rmm1613rmm1613 Posts: 10Questions: 0Answers: 0
edited May 2011 in General
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!

Replies

  • rmm1613rmm1613 Posts: 10Questions: 0Answers: 0
    Anybody?? Any help is much appreciated!
  • ChrisGedrimChrisGedrim Posts: 10Questions: 0Answers: 0
    Is this what you wanted?

    [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]
  • ChrisGedrimChrisGedrim Posts: 10Questions: 0Answers: 0
    Actually... you'd need to flatten the array before returning it. My bad
  • rmm1613rmm1613 Posts: 10Questions: 0Answers: 0
    Thanks!!

    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
  • ChrisGedrimChrisGedrim Posts: 10Questions: 0Answers: 0
    It probably does... I threw it together in a couple of minutes over lunch :D

    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
  • rmm1613rmm1613 Posts: 10Questions: 0Answers: 0
    I'm a datatables noob so I will take your direction and try to apply it to the format datatables is looking for in custom sorting functions if I don't receive any additional help on here.

    You were a great help. Thanks again!
This discussion has been closed.