-1

I have an object like the below, i want to map this array in a particular order like, Argentina, Chilean, United States

1: {label: 'Turkish Lira', flag: 'TR', id: 1, code: 'TRY', symbol: '₺', …}
2: {label: 'United States Dollar', flag: 'US', id: 2, code: 'USD', symbol: '$', …}
3: {label: 'Argentina Peso', flag: 'AR', id: 3, code: 'ARS', symbol: '$', …}
4: {label: 'Bahraini Dinar', flag: 'BH', id: 4, code: 'BHD', symbol: ' BD', …}
5: {label: 'Brazilian Real', flag: 'BR', id: 5, code: 'BRL', symbol: ' R$', …}
6: {label: 'Bulgarian Lev', flag: 'BG', id: 6, code: 'BGN', symbol: ' лв', …}
7: {label: 'Chilean Peso', flag: 'CL', id: 7, code: 'CLP', symbol: '$', …}

How can i do that?

nodabasi
  • 58
  • 6
  • https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value – Alex028502 Oct 15 '21 at 06:34
  • So you want to sort it? If you can write us a function that takes two country names and says country X comes before country Y you can use the `[].sort(cmpFn)` method – nlta Oct 15 '21 at 06:35
  • 2
    Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – Balastrong Oct 15 '21 at 06:36
  • That's what I said @Balastrong - how do you format your link like that? – Alex028502 Oct 15 '21 at 06:37
  • @Alex028502 You can see all formatting options when you click the "help" text next to the comment-field. – cloned Oct 15 '21 at 06:38
  • I don't want to sort it i have an array and i have an order in my hand that i want to map it. There is no such a rule for my order i randomly select the items. For example, index of 3,5,0,2,9. How can i map them – nodabasi Oct 15 '21 at 06:39
  • You can hit the `flag` button, select `a duplicate` and search for the question you want to appear here. This also helps closing the question if it gets accepted. – Balastrong Oct 15 '21 at 06:39
  • Then please update the question and write all the details :) – Balastrong Oct 15 '21 at 06:40
  • @nodabasi could you show what the end result should look like? – Alex028502 Oct 15 '21 at 06:42

2 Answers2

1

Given you have a list of items you want to sort, called items. You have a list of indexes, such as [0, 4, 2, 1, 3]

You can indexes.map(i => items[i]);

var items = [{
    label: 'Turkish Lira',
    flag: 'TR',
    id: 1,
    code: 'TRY',
    symbol: '₺'
  }, {
    label: 'United States Dollar',
    flag: 'US',
    id: 2,
    code: 'USD',
    symbol: '$'
  },
  {
    label: 'Argentina Peso',
    flag: 'AR',
    id: 3,
    code: 'ARS',
    symbol: '$',
  },
  {
    label: 'Bahraini Dinar',
    flag: 'BH',
    id: 4,
    code: 'BHD',
    symbol: ' BD'
  },
  {
    label: 'Brazilian Real',
    flag: 'BR',
    id: 5,
    code: 'BRL',
    symbol: ' R$'
  },
  {
    label: 'Bulgarian Lev',
    flag: 'BG',
    id: 6,
    code: 'BGN',
    symbol: ' лв'
  },
  {
    label: 'Chilean Peso',
    flag: 'CL',
    id: 7,
    code: 'CLP',
    symbol: '$'
  }
]

var indexesList = [3, 5, 0, 2];

function sortByIndex(list, indexes) {
  return indexes.map(i => items[i]);
}

console.log(sortByIndex(items, indexesList));
Balastrong
  • 4,336
  • 2
  • 12
  • 31
0

You can use javascript array sort

const array = [{
  label: 'Turkish Lira',
  flag: 'TR',
  id: 1,
  code: 'TRY',
  symbol: '₺'
}, {
  label: 'United States Dollar',
  flag: 'US',
  id: 2,
  code: 'USD',
  symbol: '$'
}, {
  label: 'Argentina Peso',
  flag: 'AR',
  id: 3,
  code: 'ARS',
  symbol: '$'
}]

function sortBy(a, b) {
  if (a.label < b.label) {
    return -1;
  }
  if (a.label > b.label) {
    return 1;
  }
  return 0;
}

console.log(array.sort(sortBy));

If you want a generic functionality then you can do this.

const array = [{
  label: 'Turkish Lira',
  flag: 'TR',
  id: 1,
  code: 'TRY',
  symbol: '₺'
}, {
  label: 'United States Dollar',
  flag: 'US',
  id: 2,
  code: 'USD',
  symbol: '$'
}, {
  label: 'Argentina Peso',
  flag: 'AR',
  id: 3,
  code: 'ARS',
  symbol: '$'
}]

const sortBy = (attribute) => (a, b) => {
  if (a[attribute] < b[attribute]) {
    return -1;
  }
  if (a[attribute] > b[attribute]) {
    return 1;
  }
  return 0;
}

console.log(array.sort(sortBy('label')));
console.log(array.sort(sortBy('code')));
Mehul Thakkar
  • 2,177
  • 13
  • 22