Sorting money Brasil

Sorting money Brasil

eduhutteneduhutten Posts: 7Questions: 1Answers: 0
edited August 2009 in Plug-ins
How do you sort of money in the Brazil. someone help me? thanks

ex: 1.500,00
20.000,00
30,00

Replies

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Hi eduhutten,

    You need to make use of a custom sorting function - http://datatables.net/plug-ins . I don't think there is one already written from Brazilian formatting, but I think it will be fairly easy for you to modify the "comma's for decimal place" one to match your needs.

    Regards,
    Allan
  • eduhutteneduhutten Posts: 7Questions: 1Answers: 0
    hello Allan

    was exactly what I imagined Sorting modify the decimal place, but I work with php I do not know how this change ... Someone there can help me?
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Hi,

    Okay - let's take the comma's for decimal place example. What you need to do is first remove the '.'s in your numbers and then change the ','s into dots. Here is an example for the ascending function:

    [code]
    jQuery.fn.dataTableExt.oSort['brazilian-asc'] = function(a,b) {
    var x = (a == "-") ? 0 : a.replace( /./, "" ).replace( /,/, "." );
    var y = (b == "-") ? 0 : b.replace( /./, "" ).replace( /,/, "." );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };
    [/code]
    See how you go with that.

    Allan
  • eduhutteneduhutten Posts: 7Questions: 1Answers: 0
    thank you Allan ...
    sorry that did not work did not know if something wrong
    was so
    [code]
    jQuery.fn.dataTableExt.oSort['brazilian-asc'] = function(a,b) {
    var x = (a == "-") ? 0 : a.replace( /./, "" ).replace( /,/, "." );
    var y = (b == "-") ? 0 : b.replace( /./, "" ).replace( /,/, "." );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['brazilian-desc'] = function(a,b) {
    var x = (a == "-") ? 0 : a.replace( /./, "" ).replace( /,/, "." );
    var y = (b == "-") ? 0 : b.replace( /./, "" ).replace( /,/, "." );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };
    [/code]
    and page
    { "bSortable": true, "sType": "brazilian" },

    sorry to bothered, you are very nice
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Hi,

    [code]
    jQuery.fn.dataTableExt.oSort['brazilian-asc'] = function(a,b) {
    var x = (a == "-") ? 0 : a.replace( /\./, "" ).replace( /,/, "." );
    var y = (b == "-") ? 0 : b.replace( /\./, "" ).replace( /,/, "." );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['brazilian-desc'] = function(a,b) {
    var x = (a == "-") ? 0 : a.replace( /\./, "" ).replace( /,/, "." );
    var y = (b == "-") ? 0 : b.replace( /\./, "" ).replace( /,/, "." );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };
    [/code]
    I think I had an error in my example - the '.' needs to be escaped in the search! Try that.

    Allan
  • eduhutteneduhutten Posts: 7Questions: 1Answers: 0
    Allan perfect !!!
    Thank you for all your assistance.
    I truly appreciate … your help in resolving the problem.
  • wilsonrnetowilsonrneto Posts: 6Questions: 1Answers: 0
    Hi Allan,
    I made a update in your function to work fine with values over 999.999,99

    [code]
    jQuery.fn.dataTableExt.oSort['brazilian-asc'] = function(a,b) {
    var x = (a == "-") ? 0 : a.replace( /\./g, "" ).replace( /,/, "." );
    var y = (b == "-") ? 0 : b.replace( /\./g, "" ).replace( /,/, "." );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['brazilian-desc'] = function(a,b) {
    var x = (a == "-") ? 0 : a.replace( /\./g, "" ).replace( /,/, "." );
    var y = (b == "-") ? 0 : b.replace( /\./g, "" ).replace( /,/, "." );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };
    [/code]

    Wilson Rocha Neto
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    NIce one! Thanks for the update :-)

    Allan
This discussion has been closed.