jQuery on page load sorting will not sort with leading currency symbol

In my previous question I was trying to sort a table on page load without any client side table sorting toggle switches just so the table would be sorted for a user as they arrive on the page.

The question was answered and I got a pretty good response and thought i'd solved the issue so I went to sleep, so as I tried to use the code on my website today I realized the sorting technique is not working but works perfectly in jsfiddle https://jsfiddle.net/ghzch66e/12/.

So I then realized the table wont sort on my website because on the webpage the data contains a leading (?) symbol https://jsfiddle.net/ghzch66e/13/.

How can I make the table sort even with a leading (?) symbol.

jQuery(document).ready(function(e) {
var dataRows = [];

//Create an array of all rows with its value (this assumes that the amount is always a number.  You should add error checking!!  Also assumes that all rows are data rows, and that there are no header rows.  Adjust selector appropriately.
$('#internalActivities > tbody > tr').each(function(i,j) {
   dataRows.push({'amount': parseFloat($(this).find('.amount').text()), 'row': $(this)});
})

//Sort the data smallest to largest
dataRows.sort(function(a, b) {
   return a.amount - b.amount;
});

//Remove existing table rows.  This assumes that everything should be deleted, adjust selector if needed :).
$('#internalActivities').empty();

//Add rows back to table in the correct order.
dataRows.forEach(function(ele) {
   $('#internalActivities').append(ele.row);
})
});

Answers 1

  • replace the "?" with "" when pushing it to the array

    $('#internalActivities > tbody > tr').each(function(i,j) {
            dataRows.push({'amount': parseFloat($(this).find('.amount').text().replace(/?/,"")), 'row': $(this)});
          })
    

Related Articles