1

I am trying to do the following.

strs = ["one", "two"];
let sorted_str = strs.map((s) => [s.sort(), s]);

Essentially, what I am trying to do is create a new array of arrays, where this new array is like [sorted string from first array, original string from first array]

However, it looks like the .sort() method is not valid here.

I even tried making it

let sorted_str = strs.map((s) => [s.toString().sort(), s]);

to force a String and make sure it has a sort() method possible, but to no avail.

The error is TypeError: s.toString(...).sort is not a function.

Any way I can get this to work or any simple workaround will be appreciated.

5 Answers5

5

You need to get an array of characters, sort it and get a string back.

const
    strs = ["one", "two"],
    sorted_str = strs.map((s) => [Array.from(s).sort().join(''), s]);

console.log(sorted_str);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

You need to convert to array, then apply the sort method, and then join to a string again, try this:

strs = ["one", "two"];
let sorted_str = strs.map((s) => [s.split('').sort().join(''), s])

console.log(sorted_str);
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
2

instead of using split(). do not use split()

strs = ["one", "two"];
let sorted_str = strs.map((s) => [[...s].sort().join(''), s]);

console.log(sorted_str)
ashen madusanka
  • 647
  • 1
  • 9
  • 15
1

The String class does not have a .sort() method. The array class does, though! So you can convert the string to an array, sort that, and then convert the sorted array back to a string. That would look something like

s.split("").sort().join("")
EKW
  • 2,059
  • 14
  • 24
1

The sort() method needs an array as input, but if you call it in the callback function passed to a map() you are calling it on a single string.

I'm not 100% sure that I understood the desired output, anyway here is my solution.

Let's start with an array of Strings that we want to sort.

const items = ['cat', 'dog', 'elephant', 'bee', 'ant'];

Now in order to sort it without mutating it we can do:

const sortedItems = [...items].sort();

Now we have two arrays, the original one, and another one with the sorted strings.

We can use map() to loop over the original array and to create the array that you need. The index parameter in the callback function can be used to retrieve the item in the sorted array.

const newItems = items.map((item, index) => {
  return [sortedItems[index], item];
});

Now if I understood correctly we have what you needed:

[
  ['ant', 'cat'], // 'ant' (from the sorted array), 'cat' (from the original)
  ['bee', 'dog'],  // same thing
  ['cat', 'elephant'], // etc.
  ['dog', 'bee'],
  ['elephant', 'ant']
]

If you want you can put everything together in one function:

const sortingMethod = (items) => {
  const sortedItems = [...items].sort();
  return items.map((item, index) => {
    return [sortedItems[index], item];
  });
}

That you can call like this:

const newElements = sortingMethod(['cat', 'dog', 'elephant', 'bee', 'ant']);
console.log(newElements);
Giorgio Tempesta
  • 1,816
  • 24
  • 32