2

I have an array that looks like this:

var arr = [
   {name: 'Price'},
   {name: 'Sale'},
   {name: 'Shipping'},
   {name: 'Customer Rating'}
]

Is there a way to sort this array so that a specific item is at a given index? For example, I always want 'Customer Rating' to be at index 1.

var arr = [
   {name: 'Price'},
   {name: 'Customer Rating'}
   {name: 'Sale'},
   {name: 'Shipping'}
]

Bonus points for es6 one liner solutions!

MarksCode
  • 8,074
  • 15
  • 64
  • 133
  • 1
    I think you'll have to explain more thoroughly what you mean by "sort this array so that a specific item is at a given index?" - Of course there is, but how are you sorting it currently? It seems like a pretty broad question to me. – zfrisch Jul 12 '18 at 18:27
  • We don't know what order the array given to us is in, and we want to transform this array into one where a specific element is always at a given index. – MarksCode Jul 12 '18 at 18:28
  • So you want to find an element with a given name and move it to an index? – Ry- Jul 12 '18 at 18:28
  • 1
    looks like you already sorted it yourself – dave Jul 12 '18 at 18:29

4 Answers4

3

Find element with name:

let i = arr.findIndex(item => item.name === 'Customer Rating');

Remove item:

let [item] = arr.splice(i, 1);

Insert item:

arr.splice(1, 0, item);

Don’t put this in one line:

arr.splice(1, 0, ...arr.splice(arr.findIndex(x => x.name=='Customer Rating'), 1));

let arr = [
   {name: 'Price'},
   {name: 'Sale'},
   {name: 'Shipping'},
   {name: 'Customer Rating'}
];

let i = arr.findIndex(item => item.name === 'Customer Rating');
let [item] = arr.splice(i, 1);
arr.splice(1, 0, item);
console.log(arr);
Ry-
  • 218,210
  • 55
  • 464
  • 476
0

Using map and find:

var arr = [
   {name: 'Price'},
   {name: 'Sale'},
   {name: 'Shipping'},
   {name: 'Customer Rating'}
];

var sorted = ['Price', 'Customer Rating', 'Sale', 'Shipping']
.map(e => arr.find(({name}) => name === e));

console.log(sorted);
Kosh
  • 16,966
  • 2
  • 19
  • 34
  • 1
    That looks quadratic time. Also not sure that the OP has `['Price', 'Customer Rating', 'Sale', 'Shipping']`. – Ry- Jul 12 '18 at 18:35
  • This only returns the items specified in the array. I think the idea was to specify the locations of certain objects, not all of them. – zfrisch Jul 12 '18 at 18:36
  • It guarantees order of all the elements, not just one. Anyway, thanks for comments! – Kosh Jul 12 '18 at 18:36
0

Find an object based on a passed in name, remove it, and then add it back in at the specified index. You can do this using splice

let setIndex = (arr, index, namevalue) => {
 arr.splice(arr.findIndex( ({name}) => name == namevalue ), 1);
 arr.splice(index, 0, {name: namevalue});
 return arr;
}

var arr = [
   {name: 'Price'},
   {name: 'Sale'},
   {name: 'Shipping'},
   {name: 'Customer Rating'}
]

let setIndex = (arr, index, namevalue) => {
  arr.splice(arr.findIndex( ({name}) => name == namevalue ), 1);
  arr.splice(index, 0, {name: namevalue});
  return arr;
}

arr = setIndex(arr, 0, 'Customer Rating');

console.log(arr);
zfrisch
  • 8,474
  • 1
  • 22
  • 34
0

What you are looking for is a swap rather than a sort.

Like the answer above, you will need to find the element first.

let i = arr.findIndex(item => item.name === 'Customer Rating');

Then jump over to this answer for the rest.

Javascript swap array elements

Ry-
  • 218,210
  • 55
  • 464
  • 476
brysgo
  • 838
  • 1
  • 8
  • 20