Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

sort array based on other array js

I have 2 lists/arrays:

[Examples]:

let labels = ["John", "Sophie", "Hannah", "Emilia"]
let data = [10, 40, 24, 25]

I want to sort both arrays with the order of data. (Each name corresponds with the value in the other array at the same index). I would use an object like this {name: value} but for my needs, they have to be seperate (at least the result). I have found a solution but i find it isnt very elegant.

[Desired Output Example]:

let data = [40, 25, 24, 10]
let labels = ["Sophie", "Emilia", "Hannah", "John"]

basically the other array has to be sorted with the same pattern that the data array will be sorted (numbers ascending)

Answer*

Draft saved
Draft discarded
Cancel

lang-js

AltStyle によって変換されたページ (->オリジナル) /