-2

I want to sort a list like this...

blue, 56 The Drive, colour red, my number is 7, green

...into...

56 The Drive, blue, colour red, green, my number is 7

i.e. numbers first, then letters. Notice that "my number is 7" does not have 7 at the beginning of the sentence and so should be sorted according go the letters.

Ideally, I want to create a function that lets me pass a multiline string, have it sort in a-z or z-a. I've been trying to sort(), reverse() join("\n") and split("\n") but getting nowhere fast.

Any ideas?

Update: The following code almost works. What is wrong is that it will show the capitals first e.g. Alpha, Zoo, age, zap. I want it to show age, Alpha, zap, Zoo. So how can I modify my code to ignore the Capitalisation part of the sort?

    if (status === 'A-Z') {
      tmpStr = tmpStr.split('\n').sort().join('\n');
    } else {
      tmpStr = tmpStr.split('\n').sort().reverse().join('\n');
    }

    return tmpStr;
  };```
Jon
  • 501
  • 3
  • 18

1 Answers1

2

Array sorting behaves exactly as you describe in your example

var input = `blue
56 The Drive
colour red
my number is 7
green`

var arr = input.split('\n');
console.log(arr.sort())

As @charlietfl points out, if you'd like numeric tie breaking for example, for strings that start with digits), you could implement a custom sorting function of the kind here:

var arr = ['Blue', '56 The Drive', 'colour red', 'my number is 7', 'Green', '250 The Drive'];
console.log(
    arr.sort(
        function(a, b) {
            if (a.match(/^\d.*/) && b.match(/^\d.*/)) {
                return parseInt(a) - parseInt(b);
            }
            return a.toLowerCase().localeCompare(b.toLowerCase());;
        }))

Note: This will still break down if the string doesn't start with a digit (e.g. a 200 will be after a 1000). It'd be great if you clarified the expected inputs and outputs in your question some more, if that is the case. The snippet above is similar to how GNU sort -n behaves for a single field.

Jedi
  • 3,088
  • 2
  • 28
  • 47
  • 3
    Not really. Try adding `"250 some text"` and it will get sorted ahead of `"56 The Drive"` due to default string sort – charlietfl Jan 23 '21 at 16:43
  • Good point that I should have checked with @Jon. I believe OP meant "numbers" in the sense of digits before alphabetic characters; but if the intention is to sort numerically (e.g. 56 before 250, then this solution does not work). – Jedi Jan 23 '21 at 16:47
  • I've confused myself a bit but got some code working. However, if you look at the Update section on my main question, I have an issue with the Capital letters sorting aspect. – Jon Jan 25 '21 at 19:32
  • As mentioned above, it'd be great to have a minimal runnable snippet in your question (similar to this answer, somewhere we can click run). It is unclear what the variables in your updated question contain (e.g. what's in `status`, `tmpStr`). I updated this answer to support case-insensitive comparison. – Jedi Jan 25 '21 at 22:22
  • I found a solution here in the end: [link](https://stackoverflow.com/questions/8996963/how-to-perform-case-insensitive-sorting-in-javascript) – Jon Jan 26 '21 at 11:57