0

I have the following array of JSON objects:

[
  {
    term: 'summer'
  },
  {
    term: 'winter'
  },
  {
    term: 'fall'
  },
  {
    term: 'summer'
  },
  {
    term: 'summer'
  }
]

I was wondering how can order this array to be arranged by specific order of the term keys. For example the requested order is object with winter will be first then fall and then summer. So the expected result should be:

[
  {
    term: 'winter'
  },
  {
    term: 'fall'
  },
  {
    term: 'summer'
  },
  {
    term: 'summer'
  },
  {
    term: 'summer'
  }
]

Please advise how can I order the array to result in the expected array.

David Faizulaev
  • 4,651
  • 21
  • 74
  • 124
  • 1
    JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. That's just an array of objects, no JSON in sight. – T.J. Crowder Sep 15 '22 at 12:31
  • Please [search thoroughly](/search?q=%5Bjs%5D+sort+object+array) before posting. More about searching [here](/help/searching). – T.J. Crowder Sep 15 '22 at 12:32
  • [Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) is typically employed like in `a.sort((o1, o2)=>seasons.indexOf(o1.term) - seasons.indexOf(o2.term))` where `seasons` should be your season names in the funny order you want, i.e., ["winter", "fall", "summer"] :-? – kikon Sep 15 '22 at 12:34
  • @T.J.Crowder the sorting I need it not alphabetical or ascending/descending , its in a specific order. Unfortunately the examples in your link do not answer the need. – David Faizulaev Sep 15 '22 at 12:39
  • @kikon the sorting I need it not alphabetical or ascending/descending , its in a specific order. Unfortunately the examples in your link do not answer the need. – David Faizulaev Sep 15 '22 at 12:40
  • Where is `spring`? – connexo Sep 15 '22 at 12:40
  • You can create a second array with the elements in the order that you want like this `const orderArray = ['winter', 'fall', 'summer'];` and then compare it with your current array. I would gladly answer but question was closed – Chris G Sep 15 '22 at 12:41
  • @DavidFaizulaev - try it out - it does sort in your desired order. `Array.sort`'s argument is a function that implements your condition. It doesn't have to be neither numerical nor alphabetical - and it isn't in the example code I posted – kikon Sep 15 '22 at 12:42
  • @DavidFaizulaev - There are several with such a restriction as well. Sorry I marked the wrong one originally. – T.J. Crowder Sep 15 '22 at 13:06
  • @T.J.Crowder I searched for similar posts but was unable to find them. Apologize for any inconvenience. – David Faizulaev Sep 15 '22 at 17:52

1 Answers1

2

You can use Array#sort with a custom comparer function like so:

const data = [
  {
    term: 'summer'
  },
  {
    term: 'winter'
  },
  {
    term: 'fall'
  },
  {
    term: 'summer'
  },
  {
    term: 'summer'
  }
]

const SEASON_ORDER = ['winter', 'fall', 'summer', 'spring']

const seasonComparer = ({term: a}, {term: b}) => 
    SEASON_ORDER.indexOf(a) - SEASON_ORDER.indexOf(b)

console.log(data.sort(seasonComparer))

For custom comparer functions with parameters a and b, if the return value is:

  • greater than 0, then a is sorted to be after b
  • less than 0, then a is sorted to be before b
  • equal to 0, then the existing order is maintained

Note that if the ordering list is large, then you might like to use a map instead (constant time), to avoid the O(N) look-up time associated with Array#indexOf.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • 1
    Duplicate of https://stackoverflow.com/questions/18859186/sorting-an-array-of-javascript-objects-a-specific-order-using-existing-function and probably several others. – T.J. Crowder Sep 15 '22 at 13:06