-1

With the following jQuery code to order table rows, numbers with dot for thousands formatting are not ordering correctly:

These numbers:

1.000, 2.500, 4.000, 850

are ordered as:

850, 4.000, 2.500, 1.000

I need order these sample numbers without remove the dot.

The jQuery code:

$('th').each(function (column) {
    $(this).addClass('sortable').click(function () {
        var findSortKey = function ($cell) {
            return $cell.find('.sort-key').text().toUpperCase()+ ' ' + $cell.text().toUpperCase();

        };
        var sortDirection = $(this).is('.sorted-asc') ? -1 : 1;
        var $rows = $(this).parent().parent().parent().find('tbody tr').get();
        var bob = 0;
        // Loop through all records and find
        $.each($rows, function (index, row) {
            row.sortKey = findSortKey($(row).children('td').eq(column));
        });

        // Compare and sort the rows alphabetically or numerically
        $rows.sort(function (a, b) {
            if (a.sortKey.indexOf('-') == -1 && (!isNaN(a.sortKey) && !isNaN(a.sortKey))) {
                 // Rough Numeracy check

                    if (parseInt(a.sortKey) < parseInt(b.sortKey)) {
                        return -sortDirection;
                    }
                    if (parseInt(a.sortKey) > parseInt(b.sortKey)) {
                        return sortDirection;
                    }

            } else {
                if (a.sortKey < b.sortKey) {
                    return -sortDirection;
                }
                if (a.sortKey > b.sortKey) {
                    return sortDirection;
                }
            }
            return 0;
        });

        // Add the rows in the correct order to the bottom of the table
        $.each($rows, function (index, row) {
            $('tbody').append(row);
            row.sortKey = null;
        });

        // Identify the column sort order
        $('th').removeClass('sorted-asc sorted-desc');
        var $sortHead = $('th').filter(':nth-child(' + (column + 1) + ')');
        sortDirection == 1 ? $sortHead.addClass('sorted-asc') : $sortHead.addClass('sorted-desc');

        // Identify the column to be sorted by
        $('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');
    });
});
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

1 Answers1

0

On JavaScript there is not delimiter for thousands numbers. only for decimal and it's .

With dot separator you cant define your number variable as Int or float because this will be considered as decimal number and it remove the zero after decimal.

You can still sort your number with dot.

  • Replace dot by empty value ("")
  • Sort yours numbers
  • And add the dot delimiter as string

Please try:

var numbers = ["4.500", "1.000", "1", "5", "25", "10"];
var num = [];

$.each(numbers,function(i,e){
  dotPos = numbers[i].replace(/\./g,"")
  num.push(parseInt(dotPos))
})
num.sort(function(a, b){return a-b});
$.each(num,function(i,e){
  console.log(num[i].toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1."))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
P. Frank
  • 5,691
  • 6
  • 22
  • 50