-2

I automatically generate an array of objects in a random order. This means that the initial order of objects can be different.

var a = [{account: true},{name: true},{amount: true},{address: true}];

or

var a = [{name: true},{account: true},{amount: true},{address: true}];

How to expose objects to a given pattern?

var a = [{amount: true},{address: true},{account: true},{name: true}];

How can I arrange the automatically generated elements in a different order each time in a special order? I need the items to be in order - amount, address, account, name.

AND I have the solution!

var order = [{account: true},{name: true},{amount: true},{address: true}];
var pattern = [{amount: true},{address: true},{account: true},{name: true}];

var sort = pattern.map(pattern => order.find(order => order[0] === pattern[0]));

console.log(sort); //[{amount: true},{address: true},{account: true},{name: true}]
Vlad
  • 91
  • 5
  • I don't immediately agree with the duplicate closure. Please update your question to include a [Minimal, Complete, and Reproducible Code Example](https://stackoverflow.com/help/minimal-reproducible-example) for what you've tried on your own already to sort your data, and make more clear what it is you are wanting, or what the problem is that you are trying to solve for. SO isn't a code writing service. – Drew Reese Jul 06 '21 at 00:10
  • What kind of pattern? – Chris Jul 06 '21 at 00:11
  • How can I arrange the automatically generated elements in a different order each time in a special order? I need the items to be in order - amount, address, account, name ? – Vlad Jul 06 '21 at 00:17
  • Depending on the user's action, these elements are arranged in a different order. But in the end, I need to bring these elements in the specified order - amount, address, account, name. How can you do this? – Vlad Jul 06 '21 at 00:20
  • Will element objects ever have more than one of these properties/keys? Any preference on tie-breakers? And again, how have you tried to resolve this on your own already? – Drew Reese Jul 06 '21 at 00:21
  • Each element has only one property at all times. For example, I imagine how to sort the items alphabetically, but I don’t know how to sort the items in the specified order. So I am asking for help. – Vlad Jul 06 '21 at 00:31
  • Please do try this on your own first. I've what I think is a working solution, but as stated previously, stackoverflow isn't a code writing service, we help people with existing code that has issues. You need to put in a little effort. – Drew Reese Jul 06 '21 at 00:54
  • I have the solution! var order = [{account: true},{name: true},{amount: true},{address: true}]; var pattern = [{amount: true},{address: true},{account: true},{name: true}]; var sort = pattern.map(pattern => order.find(order => order[0] === pattern[0])); console.log(sort); //[{amount: true},{address: true},{account: true},{name: true}] – Vlad Jul 06 '21 at 20:22

1 Answers1

0

I'm not able to get a correct sort using your code. It seems to return an array with all the same elements of whatever is the first element of the order array.

[{ account: true },{ account: true },{ account: true },{ account: true }]

const order = [
  { account: true },
  { name: true },
  { amount: true },
  { address: true }
];
const pattern = [
  { amount: true },
  { address: true },
  { account: true },
  { name: true }
];

var sort = pattern.map((pattern) =>
  order.find((order) => order[0] === pattern[0])
);

console.log("unsorted: ", JSON.stringify(order));
console.log("sorted:   ", JSON.stringify(sort));

Here's a sort that does appear to sort your array element objects.

const sortedData = data.sort(
  (a, b) =>
    !!b.amount - !!a.amount ||
    !!b.address - !!a.address ||
    !!b.account - !!a.account ||
    !!b.name - !!a.name
);

This works by coercing the undefined values to a boolean (i.e. undefined -> false) and then uses numerical comparison by again coercing the boolean values to type number (true -> 1, false -> 0), which results in sort comparator result values of [-1, 0, 1].

const data = [
  { account: true },
  { name: true },
  { amount: true },
  { address: true }
];

console.log("unsorted: ", JSON.stringify(data));

const sortedData = data.sort(
  (a, b) =>
    !!b.amount - !!a.amount ||
    !!b.address - !!a.address ||
    !!b.account - !!a.account ||
    !!b.name - !!a.name
);

console.log("sorted:   ", JSON.stringify(sortedData));
Drew Reese
  • 165,259
  • 14
  • 153
  • 181