Is there a way of sorting indented, multi-line content alphabetically in Visual Studio Code (VSCode)? - sorting

I have a very long script that contains a huge list of brand names that is not in alphabetical order. The result of many different people working on it over a few years :) Question - is there a way to re-order lines of code alphabetically in Visual Studio Code (VSCode)? I know this is possible for single lines of code, but is it possible for multi-line blocks of code that have been indented? See attached example - so in this case I would be looking to select all these lines of code and sort by the brand name (acer, acqua, aeg, amazon etc) and retain the nested parts of the code for each. Many thanks in advance if anyone has any ideas or suggestions
Screenshot of code to be sorted
I tried sorting in VSCode, but it only sorts individual lines of code and mixes them together. It does not recognize multiline, nested blocks of code as being one group of code that can be sorted individually

[Assuming you have a json object or could your text into one. Looks very close already - if it isn't json you might consider making a separate file and make it json, so you can use a sorter.]
I see a number of json sorters in the Marketplace: just search for json sort.
This one has 280K+ downloads: Sort JSON Objects
And I see a couple more. There is no built-in way to do what you want, you will need an extension.

I would:
Replace new line + 3x - \n\t\t\t - with an empty string
Sort
Format document (you may need to Change Language Mode first)

Related

Tiny Elasticsearch like search library in ruby?

Is there an alternative to Elasticsearch? I am looking for something lightweight and tiny. Some sort of Ruby library would be ideal. For example, if I don't need to run an application to do the job but instead using that library I can search YAML or JSON files for matching text etc. And based on match score returns the sorted list.
Example here would make it clear.
Let's say I have a single YAML file with array of strings E.g
values:
- “some nice text here”
- “this is another sentence”
- “some nice”
- “nice text here”
So now if I search for “some nice text here” the first line should come on top but also there is a slight similarity with the last two lines, so the search results should return as follows:
some nice text here
nice text here
some nice
Thanks
Sphinx, Solr, come to mind but they're not small. Hell even using PostgreSQL until you really need to scale is fine (and you'll know when you need to scale) and it's probably already baked into your stack.
If your needs are to search YAML files, you could easily write your own naive approac
There's also https://github.com/mezis/fuzzily but it might require some updating.

Fast comparison xml files with ruby and saving to db

I'm looking for method for fast comparison xml files with ruby. I have 2 files (40MB), each about 100,000 rows. Each element consists of 20 attributes. Files obtained from different databases. I would like to compare them and find out what data has changed, to update them in the database.Tell me, what's going to help me?
With xml I've never worked, so in my head is an idea for comparison of each line (after the parsing and checking with the database). But I think it is very slow.
If files are reasonably formatted (each attire etc in new line) you can perhaps use command diff and parse output.

DUnit Compare Two Text Files and show Diff

Is there a way to compare two text files and show the diff if they are not identical in dunit?
The easy start is to read them to TStringList, however the code for comparing two text file is much more complicated, and the gui in the DUnitGui is not sufficient for this.
Any idea? suggestion?
There is a nice little unit that comes with some examples called TDiff, this is available from http://angusj.com/delphi/ and will allow you to compare 2 files and see the differences, it also allows for merging.
It is a very simple Utility that you can download the entire source for.

Eliminating code duplication in a single file

Sadly, a project that I have been working on lately has a large amount of copy-and-paste code, even within single files. Are there any tools or techniques that can detect duplication or near-duplication within a single file? I have Beyond Compare 3 and it works well for comparing separate files, but I am at a loss for comparing single files.
Thanks in advance.
Edit:
Thanks for all the great tools! I'll definitely check them out.
This project is an ASP.NET/C# project, but I work with a variety of languages including Java; I'm interested in what tools are best (for any language) to remove duplication.
Check out Atomiq. It finds code that is duplicate that is prime for extracting to one location.
http://www.getatomiq.com/
If you're using Eclipse, you can use the copy paste detector (CPD) https://olex.openlogic.com/packages/cpd.
You don't say what language you are using, which is going to affect what tools you can use.
For Python there is CloneDigger. It also supports Java but I have not tried that. It can find code duplication both with a single file and between files, and gives you the result as a diff-like report in HTML.
See SD CloneDR, a tool for detecting copy-paste-edit code within and across multiple files. It detects exact copyies, copies that have been reformatted, and near-miss copies with different identifiers, literals, and even different seqeunces of statements.
The CloneDR handles many languages, including Java (1.4,1.5,1.6) and C# especially up to C#4.0. You can see sample clone detection reports at the website, also including one for C#.
Resharper does this automagically - it suggests when it thinks code should be extracted into a method, and will do the extraction for you
Check out PMD , once you have configured it (which is tad simple) you can run its copy paste detector to find duplicate code.
One with some Office skills can do following sequence in 1 minute:
use ordinary formatter to unify the code style, preferably without line wrapping
feed the code text into Microsoft Excel as a single column
search and replace all dual spaces with single one and do other replacements
sort column
At this point the keywords for duplicates will be already well detected. But to go further
add comparator formula to 2nd column and counter to 3rd
copy and paste values again, sort and see the most repetitive lines
There is an analysis tool, called Simian, which I haven't yet tried. Supposedly it can be run on any kind of text and point out duplicated items. It can be used via a command line interface.
Another option similar to those above, but with a different tool chain: https://www.npmjs.com/package/jscpd

Windows Explorer sort method

I'm looking for an algorithm that sorts strings similar to the way files (and folders) are sorted in Windows Explorer. It seems that numeric values in strings are taken into account when sorted which results in something like
name 1, name 2, name 10
instead of
name 1, name 10, name 2
which you get with a regular string comparison.
I was about to start writing this myself but wanted to check if anyone had done this before and was willing to share some code or insights. The way I would approach this would be to add leading zeros to the numeric values in the name before comparing them. This would result in something like
name 00001, name 00010, name 00002
which when sorted with a regular string sort would give me the correct result.
Any ideas?
It's called "natural sort order". Jeff had a pretty extensive blog entry on it a while ago, which describes the difficulties you might overlook and has links to several implementations.
Explorer uses the API StrCmpLogicalW() for this kind of sorting (called 'natural sort order').
You don't need to write your own comparison function, just use the one that already exists.
A good explanation can be found here.
There is StrCmpLogicalW, but it's only available starting with Windows XP and only implemented as Unicode.
Some background information:
http://www.siao2.com/2006/10/01/778990.aspx
The way I understood it, Windows Explorer sorts as per your second example - it's always irritated me hugely that the ordering comes out 1, 10, 2. That's why most apps which write lots of files (like batch apps) always use fixed length filenames with leading 0's or whatever.
Your solution should work, but you'd need to be careful where the numbers were in the filename, and probably only use your approach if they were at the very end.
Have a look at
http://www.interact-sw.co.uk/iangblog/2007/12/13/natural-sorting
for some source code.
I also posted a related question with additional hints and pitfalls:
Sorting strings is much harder than you thought
I posted code (C#) and a description of the algorithm here:
Natural Sort Order in C#
This is a try to implement it in Java:
Java - Sort Strings like Windows Explorer
In short it splits the two Strings to compare in Letter - Digit Parts and compares this parts in a specific way to achieve this kind of sorting.

Resources