-1

I have an array of objects like this:

{
     countries: [{
        "country_alpha2_code": "PW",
        "country_name": "PALAU"
        },{
        "country_alpha2_code": "US",
        "country_name": "UNITED STATES"
        }
     ]
}

What I need done is to sort on country_name only, the two character code is not necessary, to give this:

{
     countries: [
        {
        "country_alpha2_code": "US",
        "country_name": "UNITED STATES"
        },{
        "country_alpha2_code": "PW",
        "country_name": "PALAU"
        }
     ]
}

I used lodash to make the new array of objects. This will go into a dropdown.

I tried this first:

// this.newJSONCountryArr.sort((a,b) => priorityIndex[a.country_name - b.country_alpha2_code]);

Nothing. Then this:

this.newJSONCountryArr.sort(this.compare);
console.log('New Country Arr: ', self.newJSONCountryArr);

compare(a: { country_name: string; }, b: { country_name: string; }): number{
  if (a.country_name < b.country_name) {
    return -1;
  }
  if (a.country_name > b.country_name) {
    return 1;
  }
  return 0;
}

I really don't need a number returned I simply want to sort the array of objects based on the objects I have by country_name.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Peter The Angular Dude
  • 1,112
  • 5
  • 26
  • 53
  • What do you mean "nothing"? What was the actual output? Give a proper [mre], not screenshots and vague descriptions. – jonrsharpe Jun 09 '21 at 17:14
  • Jon, see my update should answer your concern. – Peter The Angular Dude Jun 09 '21 at 17:23
  • You still haven't included the actual output for either of your two attempts. What does "nothing" mean, exactly? If you only want to sort by `country_name` why does the first attempt also mention `country_alpha2_code`? *"I really don't need a number returned"* - OK, but _the `sort` algorithm does_. Maybe read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort to get a basic idea of what's going on. – jonrsharpe Jun 09 '21 at 17:27
  • Probable dupe: https://stackoverflow.com/q/1129216/3001761. – jonrsharpe Jun 09 '21 at 17:28
  • Jon is correct. My first thought was using that algorithm. There are 241 Countries in the array of objects. Each object has country_alpha2_code (which is the international country 2 CHAR code) and the country_name. The code is irrelevant and just need to sort all the objects by country_name ascending from A - Z. They are coming from the database all scattered in no sort order. That's why I made a new array of objects with only the code to pass and the country name to appear in the drop down. Get me? – Peter The Angular Dude Jun 09 '21 at 17:46

2 Answers2

1
const countriesFromDb = [
  {
    country_alpha2_code: 'US',
    country_name: 'UNITED STATES'
  },
  {
    country_alpha2_code: 'PW',
    country_name: 'PALAU'
  }
];

const sorted = [...countriesFromDb].sort((a, b) =>
  a.country_name.localeCompare(b.country_name)
);


Andreas Turku
  • 438
  • 3
  • 9
-1

Thanks for all the help and I need to credit Andreas for pointing me in the correct direction. With a wee bit of modification to his code, this is what I did to make it work and work it most certainly did:

this.newJSONCountryArr.sort((a,b) => a.country_name.localeCompare(b.country_name));

Here's the entire snippet

async getCountryOptions(): Promise<boolean> {

  const self = this;
  let retVal: boolean;
  let priorityIndex = { country_name: 1, country_alpha2_code: 2 };

  // this.services.getCountries();
  const response = await this.services.getCountries();

  // console.log('RESPONSE AFTER AWAIT: ', response);

  this.countries = this.services.countryList;
  console.log('Did we get countries: ', this.countries);

  // Now to make a new array for only country code and name
  let results = _.map(this.countries, function (res) {
    let newResults = { country_name: res.columns[1].paramValue, country_alpha2_code: res.columns[2].paramValue };
    // console.log('New Results: ', newResults);
    self.newJSONCountryArr.push(newResults);
  });

  this.newJSONCountryArr.sort((a,b) => a.country_name.localeCompare(b.country_name));
  // this.newJSONCountryArr.sort(this.sortCountries);
  console.log('New Country Arr: ', self.newJSONCountryArr);

  if (this.countries) {
    this.meta.countryOptions.push(this.services.countryList);
    retVal = true;
  } else {
    console.log('Whoops! ')
    retVal = false;
  }
  return retVal;
}


sortCountries(): any {
  const sorted = [...this.newJSONCountryArr].sort((a, b) =>
    a.country_name.localeCompare(b.country_name)
  );
  return sorted;
}
Peter The Angular Dude
  • 1,112
  • 5
  • 26
  • 53