Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

335
Views
(javascript) How to sort an array of strings that has seasons and years?

The seasons need to be in this order: Spring, Summer, Fall, Winter and each season has the year 2025 and 2026.

All the 2025's need to be with each other and all the 2026's need to be with each other (2025 and 2026 are just examples the years can be anything: 1945, 3005, 7980, etc).

for example:

const seasonArr = ['Spring2026',' Spring2025','Summer2026','Summer2025','Fall2025','Fall2026','Winter2026','Winter2025']

let sortedArr = []

const someFunction = () => {
   ...
}

someFunction(seasonArr) // output: sortedArr = ['Spring2025', 'Summer2025', 'Fall2025', 'Winter2025', 'Spring2026', 'Summer2026', 'Fall2026', 'Winter2026']

I know I probably have to compare the years but since they're strings I'm struggling to compare just the numbers.

this is something that I thought of:

const seasonArr = ['Spring2026',' Spring2025','Summer2026','Summer2025','Fall2025','Fall2026','Winter2026','Winter2025']

let sortedArr = []

const someFunction = (seasonArr) => {
   for (const season of seasonArr) {
     let year = season.split(/([0-9]+)/)
     // unsure where to go from here
   }
}

someFunction(seasonArr)
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I split the strings into year and season, compare the year and compare the season for same years. I use an array of seasons for the order.

const seasonArr = ['Spring2026','Spring2025','Summer2026','Summer2025','Fall2025','Fall2026','Winter2026','Winter2025'];

const seasons = ['Spring', 'Summer', 'Fall', 'Winter'];

const regexp = /(.+)(\d{4})/;
const someFunction = (s) => {
   return [...s].sort((lhs, rhs) => {
     const [seasonL, yearL] = regexp.exec(lhs).slice(1);
     const [seasonR, yearR] = regexp.exec(rhs).slice(1);
     return +yearL - +yearR || seasons.indexOf(seasonL) - seasons.indexOf(seasonR);
   });
}

let sortedArr = someFunction(seasonArr);
console.log(sortedArr);

I create a shallow copy with

[...s]

to keep the original array unchanged.

Same logic with better performance for large arrays

const seasonArr = ['Spring2026','Spring2025','Summer2026','Summer2025','Fall2025','Fall2026','Winter2026','Winter2025'];

const seasons = ['Spring', 'Summer', 'Fall', 'Winter'];

const regexp = /(.+)(\d{4})/;
const someFunction = (s) => {
   return s
     .map(el => {
       const [season, year] = regexp.exec(el).slice(1);
       return [season, year, seasons.indexOf(season[0])];
     })
     .sort((lhs, rhs) => {
       return +lhs[1] - +rhs[1] || lhs[2] - rhs[2];
     })
     .map(el => el[0] + el[1]);
}

let sortedArr = someFunction(seasonArr);
console.log(sortedArr);

about 4 years ago · Juan Pablo Isaza Report

0

This solution builds on basically what you're suggesting.

First, split the values into an array of objects that have season and year. Then sort by year and the index of the season. Then put the values back together.


const seasonArr = ['Spring2026',' Spring2025','Summer2026','Summer2025','Fall2025','Fall2026','Winter2026','Winter2025']

const SEASONS = ["Spring", "Summer", "Fall", "Winter"]

function comparator(a, b) {
  if (a.year == b.year) {
    const aSeasonIndex = SEASONS.indexOf(a.season)
    const bSeasonIndex = SEASONS.indexOf(b.season)
    return aSeasonIndex - bSeasonIndex;
  }
  return a.year - b.year;
}

function seasonYearToObject(obj) {
  const matches = obj.match(/([^\d]*)(\d+)/)
  if (matches) {
    return {season: matches[1], year: matches[2]}
  }
}

function objectToSeasonYear(obj) {
  return `${obj.season}${obj.year}`
}
  
function sortByYearAndSeason(arr) {
  return arr
    .map(entry => seasonYearToObject(entry))
    .sort(comparator)
    .map(objectToSeasonYear);
}

console.log (sortByYearAndSeason(seasonArr))

about 4 years ago · Juan Pablo Isaza Report

0

You are almost done after season.split(/([0-9]+)/) with String#split()

  • Just have created seasonObj with the desired sort order for all seasons and combine Array#map(), Destructuring assignment and Array#sort()

Code:

const seasonArr = ['Spring2026', 'Spring2025', 'Summer2026', 'Summer2025', 'Fall2025', 'Fall2026', 'Winter2026', 'Winter2025']
const seasonObj = { Spring: 0, Summer: 1, Fall: 2, Winter: 3 }

const result = seasonArr
  .map((season) => season.split(/([0-9]+)/))
  .sort(([aSeason, aYear], [bSeason, bYear]) =>
      +aYear - bYear || seasonObj[aSeason] - seasonObj[bSeason])
  .map(([season, year]) => `${season}${year}`)

console.log(result)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!