Sorting a JS array

The above page tells us how to sort the items in a JavaScript array that are not key/value pairs. So you see the months sort alphabetically, but it results in out of order for the yearly sequence.

For key/value arrays in JS: How would one sort according to the keys if the order is not sorted already?

 {
	month_order:01,
	month:"January"
 },
 {
	month_order:03,
	month:"March"
 },
 {
	month_order:02,
	month:"February"
 },

etc.

This is not an array but a list. This has no order and you cannot sort it.

Presuming these objects are contained in an array e.g.

const months = [
  {
	month_order: 01,
	month:"January"
  },
  {
	month_order: 03,
	month:"March"
  },
  {
	month_order: 02,
	month:"February"
  }
]

You could sort them with

months.sort(function(a, b){
  if (a.month_order < b.month_order) return -1
  if (a.month_order > b.month_order) return 1
  return 0
})

console.dir(months)

This will output

Array(3)
  0: {month_order: 1, month: 'January'}
  1: {month_order: 2, month: 'February'}
  2: {month_order: 3, month: 'March'}

Note the order numbers have been reformatted with the zero’s removed. If you want to keep the zeros they would have to be strings e.g. month_order: "02".

2 Likes
  1. Is this outputting a new array?

  2. Where does the a. and b. come from in this code?

if (a.month_order < b.month_order) return -1
  1. If I wanted to output only the months in order would it be the following?
console.dir(months.month)
  1. No. Array.prototype.sort mutates the original array.

  2. These are the arguments that the callback that you are passing to sort is receiving. These will correspond to the array elements as they are put into the desired order (try logging them to the console).

  3. No. months is an array. It doesn’t have a month property.

Oh, here they are:

(function(a, b)

How would I output only the months in the array, in order from top to bottom?
Thanks!

Like so:

months.map(month => month.month).join(', ');
// "January, February, March"

I would likely change the property in your array of objects from month to name, then it’ll read nicer:

months.map(month => month.name).join(', ');
// "January, February, March"
1 Like

So if we had three instead of two per list, I would use “function(a,b)” to get the same results, which is assuming the script “knows” a and b are the first two in the list?

  {
    month_order: 01,
    month_name:"January",
    month_holiday:"New Years Day"
  },

a and b refer to the objects in the array. You can have an arbitrary amount of properties on each object.

1 Like

If I want to sort the same array in different ways, should I duplicate the array before doing a sort so the original remains intact, then remove the duplicate?

Yes, it’s best to make a copy before sorting, as it’s best-practice to not mutate arrays.

You can make a duplicate of the array (called items in this example) using the spread operator, before you sort.

const alphabeticalItems = [...items].sort(alphabeticalSort);
const thematicItems = [...items].sort(thematicSort);
2 Likes

I see the spread syntax here, an how you used it:

Wow!

I find that the es6-features website is a really good way to explore the different JavaScript features that arrived with ES6 from 2015.

http://es6-features.org/

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.