2

I am attempting to sort my topArr.sales from Greatest to Least.

I am using the .sort() method in order to try and accomplish this.

At the moment my console.log return value is identical to the original array and I am stuck on what I am doing wrong in order to accomplish my desired result.

My expected result is having the topArr return in order, from greatest to least. Here is am example of my desired result:

let topArr = [ 
{ sales: "494,927", store: "Online" },
{ sales: "418,883", store: "Walk in" },
{ sales: "48,883", store: "Retail Stores" },
{ sales: "28,883", store: "Appointments" },
]

Here is my code snippet:

let topArr = [
            { sales: "494,927", store: "Online" },
            { sales: "48,883",  store: "Retail Stores" },
            { sales: "418,883", store: "Walk in" },
            { sales: "28,883",  store: "Appointments" },
        ];
        
        
       topArr.sort(function (a, b) {
        return a.sales - b.sales;
    });
    
    console.log(topArr)

2
  • "494,927" - "48,883" produces NaN, which is not a useful comparison result. Commented May 27, 2020 at 18:37
  • Do you have control over the format of the sales value? If so, change to an integer. Commented May 27, 2020 at 19:03

2 Answers 2

3

Your sales is in form of a string. You need to first parse it into an integer before you can pass it to the sort's compare function.

You can try doing it as follows -

let topArr = [{
    sales: "494,927",
    store: "Online"
  },
  {
    sales: "418,883",
    store: "Walk in"
  },
  {
    sales: "48,883",
    store: "Retail Stores"
  },
  {
    sales: "28,883",
    store: "Appointments"
  },
]

topArr.sort(function(a, b) {
  var str1 = a.sales; //Taking the string a.sales into a string variable
  var str2 = b.sales;
  str1 = str1.replace(/[^\d\.\-]/g, ""); //removing the commas
  str2 = str1.replace(/[^\d\.\-]/g, "");
  return parseInt(str1) - parseInt(str2); //parsing the modified string into float and comparing them.
});

console.log(topArr);

One more thing to note is I have assumed that sales stores only integer values. If some sales store float as well, use - parseFloat(str1) > parseFloat(str2) to get the right output.

*Note - If the above replace statement isn't working, you can also try -

 str1.replace(/,/g, "");
Sign up to request clarification or add additional context in comments.

3 Comments

It should be noted that str1.replace(/,/g, '') assumes the number is an integer and that we're in a region of the world where commas don't mean decimal places. For a more robust solution, you might consider a library like Globalize where you can use the .numberParser() method.
@jbyrd Thanks for mentioning it. I didn't had an idea about commas used as decimal in other regions. If the data used interprets commas as decimal places, replacing it with "." instead of "" and using parseFloat instead of parseInt would do the job, right?
@Abhishek - well the thing is if you want this to work internationally (in more than one region), then you'd want that to be dynamic (so that it would detect what region you're in, and based on that format it correctly). That's where the library like Globalize comes in handy.
2

You need to change the comparator function to use numeric values instead of string.

let topArr = [
            { sales: "494,927", store: "Online" },
            { sales: "48,883",  store: "Retail Stores" },
            { sales: "418,883", store: "Walk in" },
            { sales: "28,883",  store: "Appointments" },
        ];
        
        
topArr.sort(function (a, b) {
        return parseInt(a.sales.replace("/,/g","")) - parseInt(b.sales.replace("/,/g",""));
 });
    
console.log(topArr)

1 Comment

It won't work if sales string stores a big integer value like - 2,786,853. Your expression - a.sales.replace(",","")) will only replace the first comma in the string. Use a.sales.replace("/,/g","")) instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.