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.

6
  • What the condition for sorting the array? Do you just want to reverse the input as an array or is there a specific ordering? Commented Oct 1, 2016 at 1:05
  • 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 Commented Oct 1, 2016 at 1:06
  • So you want: ["89a", "900b", "1x23", "y369", "24z6"] correct? I'm assuming you don't want x after y. Commented Oct 1, 2016 at 1:08
  • Your loops are inside out: you should iterate letters first (j) and then locate the matching array element (i) Commented Oct 1, 2016 at 1:16
  • @georg that makes sense! thank you Commented Oct 1, 2016 at 2:13

1 Answer 1

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"));

Sign up to request clarification or add additional context in comments.

9 Comments

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.
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.
Oh,. I like that.. I've been doing all that 4 space indenting too. .:)
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.
@Keith Whoa this is nice. Much simpler than my code. Thank you. there is a lot i can learn from it
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.