How to solve a problem with Array.sort Cannot assign to read only property '0' of object

In my React App I’m getting an error in the sort functionality

Uncaught TypeError: Cannot assign to read only property '0' of object '[object Array]'

I have no idea what the problem is and the functionality is this:

const preparedSites = useMemo(
      () =>
        sites
          .sort(sortSiteSelection(site))
          .map(elm => {
            const arr = [
              elm.siteId,
              elm.city,
              elm.state,
              elm.country,
              elm.address,
              elm.status,
            ];
            if (nearestSites.length)
              arr.push(
                nearestSites.find(s => s.closestSiteId === elm.siteId).distance,
              );
            return arr;
          })
          .sort((a, b) => a[6] - b[6]),
      [sites, site, nearestSites],
    );

The error happening on the .sort part and the ´sortSiteSelection´ is as follow

const nameCompare = (x, y) => x.name.localeCompare(y.name);
const cityCompare = (x, y) => x.city.localeCompare(y.city);
const compareCountry = s => (x, y) => {
  if (!s) return 0;

  if (x.countryCode === s.countryCode && y.countryCode === s.countryCode)
    return 0;
  else if (x.countryCode === s.countryCode) return -1;
  else if (y.countryCode === s.countryCode) return 1;
  else return x.countryCode.localeCompare(y.countryCode);
};

export const sortSiteSelection = selectedSite => {
  if (!selectedSite) {
    console.log('NULL FROM SORT SITE');
    return () => 0;
  }
  pipeSort(compareCountry(selectedSite), nameCompare, cityCompare);
};

The OBJ of site is just

{
 siteId,
 city,
 elm.state,
 country,
 address,
 status,

}

206 thoughts on “How to solve a problem with Array.sort Cannot assign to read only property '0' of object”

  1. Pingback: ivermectil pricing
  2. That is very fascinating, You’re an overly professional blogger. I’ve joined your feed and stay up for in the hunt for extra of your magnificent post. Also, I’ve shared your website in my social networks!

    Reply
  3. You are so cool! I don’t suppose I have read through something like that before.
    So good to discover somebody with some original thoughts on this subject matter.
    Really.. many thanks for starting this up. This site is one thing that’s needed on the internet, someone with a little originality!

    Reply
  4. This is the right webpage for anyone who wishes to find
    out about this topic. You realize so much its almost tough to argue with you
    (not that I really will need to…HaHa). You certainly put a brand new
    spin on a subject that has been discussed for decades. Wonderful stuff,
    just excellent!

    Reply
  5. I’m now not certain the place you are getting your information, but great topic.
    I must spend a while learning much more or understanding more.
    Thank you for magnificent information I was in search of this info for my mission.

    Reply
  6. Hi there I am so delighted I found your blog page, I really found you by mistake, while
    I was looking on Askjeeve for something else, Anyhow I am here
    now and would just like to say thanks for a tremendous post and a all round enjoyable blog (I also
    love the theme/design), I don’t have time to look over it all at the
    moment but I have saved it and also included your RSS
    feeds, so when I have time I will be back to read more, Please do keep up the fantastic work.

    Reply
  7. Pingback: prednisone rayos
  8. Pingback: soolantra for sale

Leave a Comment