I have a JS file with about 1,000 variables that looks like this:
export const FruitOrange: React.FC = () => (
<Trans id="fruit.orange">Orange</Trans>
);
export const FruitApple: React.FC = () => (
<Trans id="fruit.apple">Apple</Trans>
);
I want to sort all the variables in this to be in alphabetical order.
In other words, I want to sort the variable declarations so that FruitApple
comes before FruitOrange
.
How can I do this?
- VSCode cannot do this as of 2023.
- I have PHPStorm, and I tried configuring arrangement rules for JavaScript and TypeScript, but even though I set the Order for each matching rule to Order by name, when I reformat the code, it is not in alphabetical order.
I just need to reformat the file once; any tool that can do this automatically is acceptable; I just don’t want to hand-sort 1,000 variables.
2
Answers
Use MS Word for example
Copy and paste your code in.
Control H, search for
^p
(the trailing space is important, so you only capture the indented lines) and ‘Replace All’ them with nothing.Control H, search for
^p}
and ‘Replace All’ them with}
.Now each definition is on a single line. In the Home icon-bar thingy, go to the Paragraph section and press Sort (the A->Z icon).
Then copy-paste it back into your code editor, such as VS Code, and get it to reformat.
This should work as long as any multi-line definitions have indentation (which again, VS Code and others should do for you).
.*
toggle) replacer (ctrl+h)n(?!export)
-><<NEWLINE>>
Sort Lines Ascending
from the command pane (ctrl-shift-p)<<NEWLINE>>
->n