0

I have an object, that looks like this:

[ { lat: 46.3464475, lon: 13.7108946, temp: 4.9 },
  { lat: 46.3435078, lon: 13.5745311, temp: 4.9 },
  { lat: 46.2430966, lon: 13.5811079, temp: 5.26 },
  { lat: 46.3437653, lon: 13.5744452, temp: 4.9 },
  { lat: 46.1830688, lon: 13.712461, temp: 7.01 },
  { lat: 45.5425894, lon: 13.7225676, temp: 10.08 },
  { lat: 45.5499172, lon: 13.73, temp: 10.08 },
  { lat: 45.5447137, lon: 13.725915, temp: 10.08 },
  { lat: 45.545615, lon: 13.7271917, temp: 10.08 },
  { lat: 45.5491447, lon: 13.7250566, temp: 10.08 } ]

So I would now like to sort it by the temp parameter. I did try to save the temp inside another array, sort that one and match it to the locations, but that gives me a lot of duplicates. How would I do this?

SomeOne
  • 13
  • 4
  • 1
    Here you go: [sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) – Tân Jan 11 '20 at 17:27

3 Answers3

0

const arr = [ { lat: 46.3464475, lon: 13.7108946, temp: 4.9 },
  { lat: 46.3435078, lon: 13.5745311, temp: 4.9 },
  { lat: 46.2430966, lon: 13.5811079, temp: 5.26 },
  { lat: 46.3437653, lon: 13.5744452, temp: 4.9 },
  { lat: 46.1830688, lon: 13.712461, temp: 7.01 },
  { lat: 45.5425894, lon: 13.7225676, temp: 10.08 },
  { lat: 45.5499172, lon: 13.73, temp: 10.08 },
  { lat: 45.5447137, lon: 13.725915, temp: 10.08 },
  { lat: 45.545615, lon: 13.7271917, temp: 10.08 },
  { lat: 45.5491447, lon: 13.7250566, temp: 10.08 } ];
  
 
 const sorted = arr.sort(function(a, b){return b.temp - a.temp});
 console.log(sorted);

This should do.

Ashish Modi
  • 7,529
  • 2
  • 20
  • 35
-1

let arr = [ { lat: 46.3464475, lon: 13.7108946, temp: 4.9 },
  { lat: 46.3435078, lon: 13.5745311, temp: 4.9 },
  { lat: 46.2430966, lon: 13.5811079, temp: 5.26 },
  { lat: 46.3437653, lon: 13.5744452, temp: 4.9 },
  { lat: 46.1830688, lon: 13.712461, temp: 7.01 },
  { lat: 45.5425894, lon: 13.7225676, temp: 10.08 },
  { lat: 45.5499172, lon: 13.73, temp: 10.08 },
  { lat: 45.5447137, lon: 13.725915, temp: 10.08 },
  { lat: 45.545615, lon: 13.7271917, temp: 10.08 },
  { lat: 45.5491447, lon: 13.7250566, temp: 10.08 } ]
  
let res = arr.sort((a,b) => a.temp - b.temp)

console.log(res)
symlink
  • 11,984
  • 7
  • 29
  • 50
-1

The sort method available in the Array API is enough for this. As stated in the documentation, the signature for sort allows you to pass a function specify how to decide which element comes before the other.

So in, your case, to sort by temperature, you just have to do as follows:

const array = [ { lat: 46.3464475, lon: 13.7108946, temp: 4.9 },
  { lat: 46.3435078, lon: 13.5745311, temp: 4.9 },
  { lat: 46.2430966, lon: 13.5811079, temp: 5.26 },
  { lat: 46.3437653, lon: 13.5744452, temp: 4.9 },
  { lat: 46.1830688, lon: 13.712461, temp: 7.01 },
  { lat: 45.5425894, lon: 13.7225676, temp: 10.08 },
  { lat: 45.5499172, lon: 13.73, temp: 10.08 },
  { lat: 45.5447137, lon: 13.725915, temp: 10.08 },
  { lat: 45.545615, lon: 13.7271917, temp: 10.08 },
  { lat: 45.5491447, lon: 13.7250566, temp: 10.08 } ]
  
console.log(array.sort((a, b) => a.temp - b.temp)) // crescent
console.log(array.sort((a, b) => b.temp - a.temp)) // decrescent

Keep in mind that the sort function changes the order of the original array, causing a mutation in your application, which might be bad in some immutable environments.