Sorting Products by Name,Price

Below on Codepen is an example of 4 products and on the left are the filter options to filter by Name, Price and a range slider to show products that are less than the amount of price you select.
And there’s not any JavaScript code expect one for the value of Slider Range and wanted to ask you about this project some question.
Should I use parseFloat for the price to sort them by highest price or do like
function(a, b){return a-b}).
If anyone could give me any answer what should I use because I am a beginner to JS and would really appreciate that :slight_smile:

Your code so far does not show how you are obtaining the prices for each item’s price. If you have the prices stored in an array as numbers already, you will not need to use parseFloat. If you are planning on parsing the DOM to get the item prices, they will come back as strings, so depending on how you write your sort, you may have to use parseFloat, but not necessarily. If you have two strings which look like numbers:

var numString1 = "12";
var numString2 = "5";

You can actually use the - to subtract one from the other to get an actual number value because of the way JavaScript coerces the expression into a number.

var numString1 = "12";
var numString2 = "5";
numString1 - numString2 // yields 7

Hope this helps answer you question.

1 Like