1

I need to sort an array of objects. How can I sort this array based on key arrval[index]?

arr = [
 { id: 0, name: 'Name 1', arrval: [5, 3, 1] },
 { id: 1, name: 'Name 2', arrval: [6, 4, 3] },
 { id: 2, name: 'Name 3', arrval: [3, 2, 0] },
]

So if I want to sort it by arrval[1], ascending, the result should be:

arr = [
 { id: 2, name: 'Name 3', arrval: [3, 2, 0] },
 { id: 0, name: 'Name 1', arrval: [5, 3, 1] },
 { id: 1, name: 'Name 2', arrval: [6, 4, 3] },
]

I can already sort this if I sort it by id or name but I can't seem to make it work by arrval

asked Sep 7, 2021 at 8:27
2
  • If you know how to sort by id or name, what prevents you doing the same for the first index of arrval? Commented Sep 7, 2021 at 8:30
  • It is exactly same as you sort with id and name . arr.sort((a, b) => a.arrval[0] - b.arrval[0]) Commented Sep 7, 2021 at 8:31

5 Answers 5

1

Simple as that

const src = [
 { id: 0, name: 'Name 1', arrval: [5, 3, 1] },
 { id: 1, name: 'Name 2', arrval: [6, 4, 3] },
 { id: 2, name: 'Name 3', arrval: [3, 2, 0] },
]
const sortByKeyIdx = ([...arr], key, idx) =>
 arr
 .sort(({[key]:a}, {[key]:b}) => 
 a[idx]-b[idx])
 
console.log(sortByKeyIdx(src, 'arrval', 1))

answered Sep 7, 2021 at 8:31
Sign up to request clarification or add additional context in comments.

Comments

0

Just sort by arrval like you sort by id or name

arr.sort( (a, b) => a.arrval[0] - b.arrval[0] )
answered Sep 7, 2021 at 8:31

Comments

0

Just pass the arrval index and sort it like you normally would. You can try this solution:

const sort = index => {
 const arr = [
 { id: 0, name: 'Name 1', arrval: [5, 3, 1] },
 { id: 1, name: 'Name 2', arrval: [6, 4, 3] },
 { id: 2, name: 'Name 3', arrval: [3, 2, 0] }
 ]
 return arr.sort((x, y) => x.arrval[index] - y.arrval[index])
}
console.log(sort(2))

answered Sep 7, 2021 at 8:31

Comments

0

There is two way to do this

first:

let arr = [{
 id: 0,
 name: 'Name 1',
 arrval: [5, 3, 1]
 },
 {
 id: 1,
 name: 'Name 2',
 arrval: [6, 4, 3]
 },
 {
 id: 2,
 name: 'Name 3',
 arrval: [3, 2, 0]
 },
]
let index = 1
arr.sort((a, b) => {
 if (a.arrval[index] < b.arrval[index]) return -1
 else if (a.arrval[index] < b.arrval[index]) return 1
 else return 0
})
console.log(arr)

second:

let arr = [{
 id: 0,
 name: 'Name 1',
 arrval: [5, 3, 1]
 },
 {
 id: 1,
 name: 'Name 2',
 arrval: [6, 4, 3]
 },
 {
 id: 2,
 name: 'Name 3',
 arrval: [3, 2, 0]
 },
]
let index = 1
arr.sort((a, b) => a.arrval[1] - b.arrval[1])
console.log(arr)

Sanket Shah
3,1091 gold badge13 silver badges24 bronze badges
answered Sep 7, 2021 at 8:38

Comments

0

The accepted answer sorts by the first array value. If you need to be able to deal with ties, where then the second array value becomes decisive, ...etc, then use this:

 
const src = [
 { id: 0, name: 'Name 1', arrval: [5, 3, 3] },
 { id: 1, name: 'Name 2', arrval: [5, 3, 1] },
 { id: 2, name: 'Name 3', arrval: [5, 2, 0, 1] },
 { id: 3, name: 'Name 4', arrval: [5, 2, 0] },
]
src.sort(({arrval: a}, {arrval: b}) => {
 for (let i = 0, len = Math.min(a.length, b.length); i < len; i++) {
 if (a[i] !== b[i]) return a[i] - b[i];
 }
 return a.length - b.length;
});
console.log(src);

answered Sep 7, 2021 at 9:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.