I have two sample strings like this.
const country = [{id:1, name:"Mexico"},
{id:2, name: "Canada"},
{id:3, name:"Italy"}];
const city = [{id:19, name: "Ottava",country:{id:2, name:"Canada"}},
{id:23, name:"Roma",country:{id:3, name: "Italy}];
Then I combine these two array
var newArray = country.concat(city);
Later I use this array with select
<Select>
{newArray.map((i) => (
<Option value={i.country ? i.country.id : i.id}>
{i.name}
{i.country && ", " + i.country.name}
</Option>
))}
</Select>
I want to sort the options alphabetically (by country name). I tried many methods. However, I haven't found a solution that I can use without breaking the option values.
As a result, the output I want to get is as follows.
<Option value={2}>Canada</Option>
<Option value={2}>Ottova, Canada</Option>
<Option value={3}>Italy</Option>
<Option value={3}>Roma, Italy</Option>
<Option value={1}>Mexico</Option>