Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a table of books with titles, authors, publishers, and dates in it. Under the title column a lot of the book titles start with the word "The." How can I setup the sort within the jquery datatables plugin to ignore the first word if it is "The" when sorting this column?

Thanks.

share|improve this question
add comment

2 Answers

Maybe you can use something like this:

First you define a custom sorting type for your column for example "SongTitle". With datatables you can define new sorting type by specifying the comparison function:

$.fn.dataTableExt.oSort['SongTitle-asc']  = function(a,b) {
    // Modify your title a and your title b by putting "The" in the end
    // Return 1 if a > b
    // Return -1 if a < b
    // Return 0 if a == b
}

Remember to define the opposite function (this was for Ascending "asc" order)

$.fn.dataTableExt.oSort['SongTitle-desc']  = function(a,b) {
    return -1 * $.fn.dataTableExt.oSort['SongTitle-asc'](a,b);
}

Now to tell DataTables to use your sorting you pass the new value to aoColumns

"aoColumns": [
    { "sType": "SongTitle" },    // Title
    { "sType": "html" }          // for the next column
],
share|improve this answer
add comment

It would probably be easier to write a title jquery renderer that moves articles (The, A, An) to the end of the string.

The Sound of Music -> Sound of Music, The

You'd probably want to do the same thing for titles that begin with "A" and "An".

share|improve this answer
 
how would you go about this? did some searches and couldn't find any examples. –  Jeffrey Aug 20 '10 at 20:32
 
I'm not a jquery wizard, but JavaScript has an indexOf method. If the first characters are an article, substring the rest of the title string, and add the article to the end of the new title string. –  Gilbert Le Blanc Aug 22 '10 at 22:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.