2

I am trying to sort an array of strings based on a character inside each of those strings. So far, I have this

function doMath(s) {

  let arr = s.split(' ');
  let letterArr = [];
  let sortedArr = [];
  let n = 0;
  for (var i = 0; i < arr.length; i++) {

    n = arr[i].indexOf(arr[i].match(/[a-z]/i));
    letterArr.push(arr[i][n]);

  }
  letterArr.sort();

  console.log(letterArr);

  for (i = 0; i < arr.length; i++) {
    for (var j = 0; j <= arr[i].length; j++) {

      if (arr[i].indexOf(letterArr[j]) > -1) {
        sortedArr.unshift(arr[i]);
      }

    }
  }
  console.log(sortedArr);
}

doMath("24z6 1x23 y369 89a 900b");

The problem is shown when I log this array. If I use sortedArr.push(arr[i]);, then the output is:

["24z6", "1x23", "y369", "89a", "900b"]

However, when I use sortedArr.unshift(arr[i]);, I get the output:

["900b", "89a", "y369", "1x23", "24z6"]

I am not sure why the b comes before the a.

I just want it to be a-z for the sorting. I tried push() and it is correct but backwards (z-a). When I try unshift(), it's correct except the b and a are switched.

Makyen
  • 31,849
  • 12
  • 86
  • 121
sourlemonaid
  • 504
  • 2
  • 6
  • 19

1 Answers1

6

function doMath(s) {
   return s.split(' ').sort(function (a,b) {
      return a.match(/[a-z]/i)[0].localeCompare(b.match(/[a-z]/i)[0])})
}

console.log(doMath("24z6 1x23 y369 89a 900b"));
Keith
  • 22,005
  • 2
  • 27
  • 44
  • 1
    It would be good to supply some explanation with your code. In addition, it would be better to use the same organization as the OP is using (i.e. a `doMath()` function which returns the results). Another improvement would be to put all of that in a snippet with a `console.log()` after it so people can run it on the page a see that you are generating the correct output. – Makyen Oct 01 '16 at 01:27
  • It was done like that so people could copy paste into a console window. And then no console.log required. But I'll do an edit for the doMath – Keith Oct 01 '16 at 01:36
  • Copy & paste into a console is significantly more effort than clicking a "Run code snippet" button. Even with something so simple, people are *much* more likely to run a code [snippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) than to open the console/devtools and copy & paste your code into it. Most people seriously won't care that much. – Makyen Oct 01 '16 at 01:40
  • 1
    I must admit, not tried code snippet's.. Only been a member for a couple of days,.. I'll see if I can do it that way. – Keith Oct 01 '16 at 01:52
  • 2
    Oh,. I like that.. I've been doing all that 4 space indenting too. .:) – Keith Oct 01 '16 at 01:55
  • 1
    Snippets help with something like this (JavaScript/HTML/CSS). With a Question/Answer like this one, using a snippet will result in more up votes (and thus gaining more reputation) as people trivially verify the answer. The 4 space indenting can be done in blocks by using the code button between the quote and picture buttons in the toolbar above the edit frame. – Makyen Oct 01 '16 at 01:58
  • Note: If addressing a comment to someone other than the person who wrote the post to which your comment is attached, you should get in the habit of including their name (e.g. `@Makyen` for me). This will notify them that there is a comment they should read. If there is only one person that has made comments, then they will automatically be notified (e.g. me in this case). In addition, the author of the post to which the comments are attached is always notified (you in this case). Just a good habit to form. @-Tab can be used to auto-complete. If not needed, the `@person` may be auto-removed. – Makyen Oct 01 '16 at 02:03
  • 1
    @Keith Whoa this is nice. Much simpler than my code. Thank you. there is a lot i can learn from it – sourlemonaid Oct 01 '16 at 02:13
  • @Makyen Will do. :) – Keith Oct 01 '16 at 02:16