2

Given an object like:

accounts = [
  { bankType: "Checking", currency: "USD", amount: 123.45 },
  { bankType: "Saving", currency: "CAD", amount: 1.95 },
  { bankType: "Saving", currency: "USD", amount: 23.31 },
  { bankType: "Checking", currency: "CAD", amount: 1953.1 },
];

How do I sort by the objects properties in the array where bankType of "Checkings" are sorted first then currency of "CAD" accounts are sorted next to achieve the following result below?

// Sorted array of objects result
[
  { bankType: "Checking", currency: "CAD", amount: 1953.1 },
  { bankType: "Checking", currency: "USD", amount: 123.45 },
  { bankType: "Saving", currency: "CAD", amount: 1.95 },
  { bankType: "Saving", currency: "USD", amount: 23.31 },
];

The problem isn't about sorting it alphabetically using the built-in localeCompare function, the problem lies in having to sort by specific constant value of Checking first then by CAD second.

3 Answers 3

3

With a point system

Checking = 2

CAD = 1

console.log(
    [
        { bankType: "Checking", currency: "USD", amount: 123.45 },
        { bankType: "Saving", currency: "CAD", amount: 1.95 },
        { bankType: "Saving", currency: "USD", amount: 23.31 },
        { bankType: "Checking", currency: "CAD", amount: 1953.1 },
    ]
        .sort((a, b) => {
            const pointsA = (a.bankType === "Checking" ? 2 : 0) + (a.currency === "CAD" ? 1 : 0);
            const pointsB = (b.bankType === "Checking" ? 2 : 0) + (b.currency === "CAD" ? 1 : 0);

            return pointsB - pointsA;
        })
);

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

Comments

3

You can just compare the two in order:

accounts.sort((a, b) =>
    a.bankType.localeCompare(b.bankType) || a.currency.localeCompare(b.currency)
);

1 Comment

added clarity to the problem. my problem isn't being able to sort alphabetically by localeCompare, but by a constant value of Checking first then CAD.
0

Using Array.prototype.sort and String.prototype.localeCompare, you can sort them.

const accounts = [
  { bankType: "Checking", currency: "USD", amount: 123.45 },
  { bankType: "Saving", currency: "CAD", amount: 1.95 },
  { bankType: "Saving", currency: "USD", amount: 23.31 },
  { bankType: "Checking", currency: "CAD", amount: 1953.1 },
];

const output = accounts.sort((a, b) => {
  const bankCompare = a.bankType.localeCompare(b.bankType);
  if (bankCompare === 0) {
    return a.currency.localeCompare(b.currency);
  }
  return bankCompare;
});
console.log(output);

1 Comment

Apologies for the confusion, added clarity to the problem. My problem isn't being able to sort alphabetically by localeCompare, but by a constant value of Checking first then CAD.

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.