0

I have a code to sort array

const timeSorted = wheater.list.sort((z,x)=>
{
    return z.dt- x.dt
})
console.log(timeSorted)

Output that i get is sorted
but if i add another sort like this

const timeSorted = wheater.list.sort((z,x)=>
{
    return z.dt- x.dt
})
const tempSorted = wheater.list.sort((a,b)=>
{
    return a.main.temp - b.main.temp
})
console.log(timeSorted)

const timeSorted become tempSorted

How can i fix that?

4
  • please add the array as well. Commented Dec 18, 2017 at 12:06
  • @NinaScholz the array is from api . is too long Commented Dec 18, 2017 at 12:07
  • not the whole, just two rows with the mentioned properties. Commented Dec 18, 2017 at 12:08
  • 1
    More: minimal reproducible example and How do I ask a good question? Commented Dec 18, 2017 at 12:10

1 Answer 1

4

The array is sorted in place.

In order to not have it changed, you need to create a copy of the previous array using oldArray.slice().

const tempSorted = wheater.list.slice().sort((a,b)=>
{
    return a.main.temp - b.main.temp
})
Sign up to request clarification or add additional context in comments.

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.