2

I want to sort string inside an array, not a normal sorting because if I do so the result will be:

var a = ["dcb","acb","gfe"];
console.log(a.sort());

the answer would be:

["acb","dcb","gfe"]

which I don't want, the sorted array I want is sorting of string inside the array, For eg:

var a = ["dcb","acb","gfe"];

expected answer :

["bcd","abc","efg"]

hope my question is clear to you :)

7
  • 10
    a.map(s => s.split('').sort().join('')) Commented Jun 21, 2019 at 18:38
  • Possible duplicate of Sort all characters in a string Commented Jun 21, 2019 at 18:39
  • @JaredSmith why did you answer as a comment instead of as an answer? Commented Jun 21, 2019 at 18:46
  • 1
    alternative, more succinct syntax: a.map(s=>[...s].sort().join('')) Commented Jun 21, 2019 at 18:49
  • The .map() method iteratates through each element of the array, and code within map callback sorts that individual element. This will work. Commented Jun 21, 2019 at 18:51

3 Answers 3

4

Use Array.map() and sort each string seperatly:

const arr = ["dcb","acb","gfe"];

const result = arr.map(str => str
  .split('')
  .sort()
  .join('')
)

console.log(result);

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

4 Comments

alternative, more succinct syntax: a.map(s=>[...s].sort().join(''))
You'll still need to join the sorted array.
@RandyCasburn .split('') admittedly has more symmetry with .join('') that makes the implementation easier to understand, even if it is slightly more characters to type... I don't think "being succinct" is necessarily the most important quality here when comparing these two approaches.
@PatrickRoberts - only offered as an alternative. I fully appreciate and understand your comment. I did upvote this answer :-)
3

To perform an operation on each element of an array (in this case, sort a string), use a loop. The Array.prototype.map function is an appropriate "loop-function" when you want to map an array of length n to an array of the same length (i.e. when you want to transform each element of an array).

let a = ["dcb", "acb", "gfe"];
let sorted = a.map(a => [...a].sort().join(''));
console.log(sorted);

1 Comment

alternative, more succinct syntax: a.map(s=>[...s].sort().join(''))
0
  1. The .map() method iterates through each element of the array.

  2. Use the ES6 arrow function and Destructuring assignment operator to simplify the code, if you want to use ES5 the use regular function and use the .split('') method.

  3. The .sort() method to sort the individual characters after splitting it.

  4. The .join('') method to joining them back.

Example (ES6):

let a = ["dcb","acb","gfe"];
let sorted = a.map( s => [...s].sort().join('') );
consloe.log(sorted);

Example (ES5):

let a = ["dcb","acb","gfe"];

let sorted = a.map( 
  function(s) { 
    return s.split('').sort().join(''); 
  });

consloe.log(sorted);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.