0

I have 2 arrays. FIRST looks like:

const arrOjObjects = [
{text: "HUF", value: 300.3803314317},
{text: "GBP", value: 0.7925201485},
{text: "IDR", value: 14127.9996377796},
{text: "ILS", value: 3.5020374898},
{text: "DKK", value: 6.7633795164}]

Second looks like:

const arr = ["ILS, "DKK"]

I need to sort arrOjObjects by some rule, if the elements of the object text match the element of the array arr, then the object must be first and all other objects behind it

I need some sort rule, which will sort my arr to this state:

const arrOjObjects = [
{text: "ILS", value: 3.5020374898}, // ILS is in arr
{text: "DKK", value: 6.7633795164} //  DKK is in arr
{text: "HUF", value: 300.3803314317},
{text: "GBP", value: 0.7925201485},
{text: "IDR", value: 14127.9996377796},
]

Thanks for answers!

3
  • 1
    Could you please show us what you tried ? Commented Oct 14, 2019 at 6:48
  • do the items of the second array have to be in order of the second array? Commented Oct 14, 2019 at 7:47
  • Sorting like this ('A" things first and then not "A") is easier with two filters: X.filter(arr.includes(x.text)).concat(X.filter(!arr.includes(x.text))) Commented Oct 14, 2019 at 7:56

3 Answers 3

1

You can use Array.prototype.sort

const arrOjObjects = [
	{text: "HUF", value: 300.3803314317},
	{text: "GBP", value: 0.7925201485},
	{text: "IDR", value: 14127.9996377796},
	{text: "ILS", value: 3.5020374898},
	{text: "DKK", value: 6.7633795164}]

	const arr = ["ILS", "DKK"];

	arrOjObjects.sort((a, b) => arr.includes(b.text) - arr.includes(a.text));

	console.log(arrOjObjects);

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

3 Comments

That's nice, but I guess the callback can be simplified to arr.includes(b.text) - arr.includes(a.text)
@georg we surely can do that, however, that does not work Node environment jdoodle.com/a/1Ce3, I am not sure why.
because you have a.text - b.text there, should be the other way round.
0

i cant think of an elegant solution for your problem. But here's a straight forward way for solving your issue.

Firstly, find the elements you want according to your search array

let sorted = [];
for (let sortText of arr) {
   let res = arrOjObjects.find((obj) => {
       return obj.text === sortText;
   });
   if (res) sorted.push(res);
}

After which, slot in the remaining values that are not part of the search criteria

let remaining = arrOjObjects.filter((obj) => {
    return !arr.includes(obj.text);
});
sorted = [...sorted, ...remaining];

Comments

0

This should work

arrOjObjects.sort((a, b) => {
  if (arr.includes(a.text) && !arr.includes(b.text)) {
    return -1;
  } else if (!arr.includes(a.text) && arr.includes(b.text)) {
    return 1;
  } else {
    return a.value - b.value;
  }
});

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.