0

I have an array:

  const array = [
    { name: 'b' },
    { name: 'a' },
    { name: 'c' },
  ]

The goal here is to keep the same array but to add a new property on each object based on its position if the array was sorted:

  const array = [
    { name: 'b', sortIndex: 1 },
    { name: 'a', sortIndex: 0 },
    { name: 'c', sortIndex: 2 },
  ]

I came up with a plan to just sort the array based on the name. Then loop through it and add the index to get this:

 const array = [
   { name: 'a', sortIndex: 0 },
   { name: 'b', sortIndex: 1 },
   { name: 'c', sortIndex: 2 },
 ]

Then I would loop through the original array and do a findIndex against the sorted array to get the index but that seems like terrible code with so many loops and stuff. Any ideas on how to make this better?

export function setSortIndexes(series) {
  const clone = cloneDeep(series);
  const sorted = orderBy(clone, [
    (x) => (x.name ? x.name.toLowerCase() : null),
  ]);

  sorted.forEach((x, i) => {
    x.sortIndex = i;
  });

  series.forEach(x => {
    const index = sorted.findIndex((s) => s.name === x.name);

    if (index !== -1) {
      x.sortIndex = sorted[index].sortIndex;
    }
  });

  return series;
}
3
  • 1
    Doesn't look that bad to me: jsfiddle.net/z5cqt9La Commented Jun 14, 2021 at 23:12
  • 1
    codereview.stackexchange.com might be a better place for this question. Commented Jun 14, 2021 at 23:13
  • @ChrisG thats alot better than what i have and it works well. thanks! Commented Jun 14, 2021 at 23:21

2 Answers 2

1

Create a temporary array to sort, including the original index.
Sort by the name field
Add index to the original object.

array
  .map((e, i) => ({ name: e.name, originalIndex: i }))
  .sort((a, b) => a.name.localeCompare(b.name))
  .forEach((e, i) => array[e.originalIndex].sortIndex = i)
Sign up to request clarification or add additional context in comments.

Comments

1

you can do that:
no need to duplicate objects (or some of its elements) in a new array. an array of pointers to these objects is sufficient

const array = 
  [ { name: 'b'} 
  , { name: 'a'} 
  , { name: 'c'} 
  ] 
array                  // create a new array  
  .map(o=>({o}))      // of pointers ( o ) to refer each array objects
  .sort((a,b)=>a.o.name.localeCompare(b.o.name)) // sort 
  .forEach((_,i,a)=>a[i].o.sortIndex = i )      // add the sortIndex attr 


console.log(array)

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.