# Sorting a mixed array of numbers and strings

**URL:** <https://forum.freecodecamp.org/t/sorting-a-mixed-array-of-numbers-and-strings/512297>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 27, 2022, 9:18pm UTC](https://forum.freecodecamp.org/t/sorting-a-mixed-array-of-numbers-and-strings/512297 "2022-05-27T21:18:05Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![VayaBuzz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vayabuzz/32/263439_2.png) [@VayaBuzz](https://forum.freecodecamp.org/u/VayaBuzz)\
**Post date:** [May 28, 2022, 6:18pm UTC](https://forum.freecodecamp.org/t/sorting-a-mixed-array-of-numbers-and-strings/512297/4 "2022-05-28T18:18:12Z")

</div>

@JeremyLT and @jwilkins.oboe , thank you so much for the quick responses!  
I apologize for my late reply, but I really appreciate your advice!

Your advice harkens back to good 'ol CS fundamentals which I should not forget-- break things down into smaller problems!

This code seemed to work for me, although it may not be super elegant!

```auto
// problem: Sort this array:
let myarr1 = [1, 'ss', 11, 2, 'aa'];
// expected output:
// [1, 2, 11, 'aa', 'ss']
// extra test cases if you need them:
//let myarr2 = ['zzz', 1, 's', 11, 2, 'a', 111];
// solution:
function compareNumbers(a, b) {
  //console.log('a: ' + a);
  //console.log('b: ' + b);
  //console.log('-------');
  if (typeof a === 'number' && typeof b === 'number') {
    return a - b;
  }
  // check for num vs string
  if (typeof a === 'number' && typeof b === 'string') {
    return -1;
  }
  // check for string vs num
  if (typeof a === 'string' && typeof b === 'number') {
    return 1;
  }
  // check for string vs string
  if (typeof a === 'string' && typeof b === 'string') {
    if (a < b) return -1;
    else return 1;
  }
  return 0;
}
myarr1.sort(compareNumbers);
console.log(myarr1);

```

Again, thanks everyone for your help! I appreciate you greatly!

---

_[View the full topic](https://forum.freecodecamp.org/t/sorting-a-mixed-array-of-numbers-and-strings/512297)._
