0

My JSON file looks like this

 {
    "PlanetIdentifier": "KOI-1843.03",
    "TypeFlag": 0,
    "PlanetaryMassJpt": 0.0014,
    "RadiusJpt": 0.054,
    "PeriodDays": 0.1768913,
    "SemiMajorAxisAU": 0.0048,
    "Eccentricity": "",
    "PeriastronDeg": "",
    "LongitudeDeg": "",
    "DiscoveryYear": 2012,
    "
  },
  {
    "PlanetIdentifier": "KOI-1843.01",
    "TypeFlag": 0,
    "PlanetaryMassJpt": "",
    "RadiusJpt": 0.114,
    "PeriodDays": 4.194525,
    "SemiMajorAxisAU": 0.039,
    "Eccentricity": "",
    "PeriastronDeg": "",
    "LongitudeDeg": "",
    "DiscoveryYear": "",
  }

Now my objective is to sort the object by year and then sort them again using the RaduisJpt.

if(RaduisJpt > 2) then it is large planet
if(RaDuisJpt <1) then it is a small planet
else it is a medium

then I have to show how many small, medium and large planets were discovered each year.

2
  • 1
    So do you know about Array methods like .sort() and .filter()? What's the issue here exactly? Have you tried anything yet? Commented Aug 1, 2021 at 22:42
  • 1
    The JSON is invalid? Commented Aug 1, 2021 at 22:43

1 Answer 1

0

Use Array#sort:

const arr = [{
    "PlanetIdentifier": "KOI-1843.03",
    "TypeFlag": 0,
    "PlanetaryMassJpt": 0.0014,
    "RadiusJpt": 0.054,
    "PeriodDays": 0.1768913,
    "SemiMajorAxisAU": 0.0048,
    "Eccentricity": "",
    "PeriastronDeg": "",
    "LongitudeDeg": "",
    "DiscoveryYear": 2012,
  },
  {
    "PlanetIdentifier": "KOI-1843.01",
    "TypeFlag": 0,
    "PlanetaryMassJpt": "",
    "RadiusJpt": 0.114,
    "PeriodDays": 4.194525,
    "SemiMajorAxisAU": 0.039,
    "Eccentricity": "",
    "PeriastronDeg": "",
    "LongitudeDeg": "",
    "DiscoveryYear": 2010,
  },
    {
    "PlanetIdentifier": "KOI-1843.01",
    "TypeFlag": 0,
    "PlanetaryMassJpt": "",
    "RadiusJpt": 0.5,
    "PeriodDays": 4.194525,
    "SemiMajorAxisAU": 0.039,
    "Eccentricity": "",
    "PeriastronDeg": "",
    "LongitudeDeg": "",
    "DiscoveryYear": 2010,
  }
]
const sorted = arr.sort((a, b) => {
  let sum = a.DiscoveryYear - b.DiscoveryYear;
  return sum == 0 ? a.RadiusJpt - b.RadiusJpt : sum
})
console.log(sorted);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.