alanphil
alanphil

Reputation: 91

Sorting Array of Objects by Object Value Not Working

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.

Upvotes: 1

Views: 697

Answers (3)

sidthesloth
sidthesloth

Reputation: 1467

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, ""));
})

Upvotes: 1

brk
brk

Reputation: 50346

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)

Upvotes: 1

Zer0
Zer0

Reputation: 1690

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

Upvotes: 3

Related Questions