Sorting graphQL data in JavaScript

Very much appreciated for reading this one! It might seem like easy for you, but I am quite new, so bear with me. I have a query in GraphQL which gives me the following results:

  "data": {
"completeglobaldata": [
      {
        "increasehotdays": 43,
        "countryname": "Afghanistan"
      },
      {
        "increasehotdays": 66.1,
        "countryname": "Angola"
      },
      {
        "increasehotdays": 0,
        "countryname": "Anguilla"
      },
      {
        "increasehotdays": 0,
        "countryname": "Åland Islands"
      },
      {
        "increasehotdays": 51.9,
        "countryname": "Albania"
      },
]
}

Yet, now I want to sort the data in a descending way with JavaScript sort. The problem is that GraphQL will return objects and I do not understand how this would work.. I have been stuck on the array/sort page for some good hours now.

export const sortDescendingHotDays = (completeglobaldata) => {
  const countryTemperatures = completeglobaldata.map((country) => ({
    ...completeglobaldata,
  }));

  return completeglobaldata.sort((a, b) =>
    a.country.increasehotdays < b.country.increasehotdays ? 1 : -1
  );
};


This is obviously wrong, but it shows how lost I am. Oh, I am using React by the way. Ofcourse I could easily change the graphQL query to :

query MyQuery {
  completeglobaldata(order_by: {increasehotdays: desc}) {
    increasehotdays
    countryname
  }
}

But this is not possible in my situation.I really need to get the data and then sort it using Javascript sort

Thank you for your help 🙂

32 thoughts on “Sorting graphQL data in JavaScript”

Leave a Comment