1

I have some Objects stored in an Array.

const products = [
    {
        title: "Gigabyte GeForce RTX 2060 OC",
        originalPrice: 329.99,
        discountPrice: 292.77,
        discountAmount: "11.28%"
    },
    {
        title: "Gigabyte GPU NV RTX2060 Windforce OC 6GB Fan",
        originalPrice: 349.99,
        discountPrice: 306.99,
        discountAmount: "12.29%"
    },
    {
        title:
            "ASUS Dual GeForce RTX 2060 OC EVO Edition 6GB GDDR6 Gaming Graphics Card with Two Powerful Axial-tech Fans (DUAL-RTX2060-O6G-EVO)",
        originalPrice: 319.98,
        discountPrice: 312.99,
        discountAmount: "2.18%"
    }
];

I am trying to sort the Array based on the Objects discountAmount property. I have spent the last hour trying solutions here on StackOverflow but nothing has successfully sorted the Array, the Objects remain in the same position. I have included two of my solutions to this problem below.

Solution 1:

products.sort((a, b) => {
    const prodA = parseFloat(a["discountAmount"].replace(/%/, ""));
    const prodB = parseFloat(b["discountAmount"].replace(/%/, ""));

    if (prodA > prodB) comparsion = 1;
    else if (prodA < prodB) comparsion = -1;
    else return 0;
});

Solution 2:

function compareValues(key, order = "asc") {
    return (a, b) => {

        // Just Checking if The Key Exists
        if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
            return 0;
        }

        let prodA = typeof a[key] === "string" ? a[key].toUpperCase() : a[key];
        let prodB = typeof b[key] === "string" ? b[key].toUpperCase() : b[key];

        prodA = prodA.replace(/%/, "");
        prodB = prodB.replace(/%/, "");

        let comparison = 0;

        if (prodA > prodB) comparsion = 1;
        else if (prodA < prodB) comparsion = -1;

        return order === "desc" ? comparison * -1 : comparison;
    };
}

Apologies in advance if there is a clear misunderstanding here.

5
  • In addition to getting rid of the % you have to turn the values into numbers before comparing them Commented Aug 31, 2020 at 14:16
  • also spell comparison consistently ("comparsion") Commented Aug 31, 2020 at 14:17
  • Solution 1 only ever returns undefined or zero. Commented Aug 31, 2020 at 14:17
  • you need to return.. playcode.io/661834 Commented Aug 31, 2020 at 14:18
  • In Solution 2 you're checking the type of a[key]/b[key] but in the next line you just assume that they are always strings (or anything with a .replace() method that accepts a regular expression and a string) - so why test them in the first place? Commented Aug 31, 2020 at 14:24

3 Answers 3

3

You can use a very short arrow function with parseFloat for that, just use:

products.sort((a, b) => parseFloat(a.discountAmount) - parseFloat(b.discountAmount)

This sorts ascending, if you want to sort descending simply switch a and b in the return of the arrow function

Sign up to request clarification or add additional context in comments.

Comments

1

Just return the difference of prodA and prodB

const products = [{
    title: "Gigabyte GeForce RTX 2060 OC",
    originalPrice: 329.99,
    discountPrice: 292.77,
    discountAmount: "11.28%"
  },
  {
    title: "Gigabyte GPU NV RTX2060 Windforce OC 6GB Fan",
    originalPrice: 349.99,
    discountPrice: 306.99,
    discountAmount: "12.29%"
  },
  {
    title: "ASUS Dual GeForce RTX 2060 OC EVO Edition 6GB GDDR6 Gaming Graphics Card with Two Powerful Axial-tech Fans (DUAL-RTX2060-O6G-EVO)",
    originalPrice: 319.98,
    discountPrice: 312.99,
    discountAmount: "2.18%"
  }
];


let sorted = products.sort((a, b) => {
  const prodA = parseFloat(a["discountAmount"].replace(/%/, ""));
  const prodB = parseFloat(b["discountAmount"].replace(/%/, ""));
  return prodA - prodB;
});


console.log(sorted)

Comments

1

The following sorts the products in the ascending order,

products.sort((a, b) => {
    return Number(a.discountAmount.replace(/%/g, "")) - Number(b.discountAmount.replace(/%/g, ""));
})

Explanation

  1. Remove the % symbol.
  2. Converting the string to Number and then compare them.

For descending, you can do,

products.sort((a, b) => {
    return Number(b.discountAmount.replace(/%/g, "")) - Number(a.discountAmount.replace(/%/g, ""));
})

Comments

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.