0

I have a really large csv file and want to sort them external.
The problem is that i need several sorting orders and a separator which I can specify.

I've searched a while and found out, that I need to use the window sort command.
But I didn't find anything. How I can specify the separator of the sorting order?

All I found was a solution on unix ("sort --field-separator=';' --key=2,1,3") but it looks like there is no equivalent on windows.

Do you have any idea how i can solve the problem without implementing my own sort algorithm?
(By the way I'm using C#)

6
  • Is there a good reason for wanting to use a Windows sort program. It would be the work of moments to write one as a C# console application. And when you say large. how big it it. Commented May 12, 2014 at 7:40
  • You just need a comparison algorithm. Use split to separate the fields. Containers like list already have a sort. Alternatively use OrderBy in LINQ. Commented May 12, 2014 at 7:43
  • If this question is about Windows programs then the question is off-topic, certainley for this site. If you want to write a C# program to do some sorting then its on topic but that program needn't run on Windows. Commented May 12, 2014 at 7:50
  • you say, you have "a" csv file. Only one? really? Why don't you use excel then? Commented May 12, 2014 at 8:11
  • I need it as part of my C# programm. The program gets a csv file, need to sort the file and store the sorted file in an archive. After that the sorted file is converted into another file format which can be read directly into the database with already existing funktions. Linq is no option cause it need to run with .Net 2.0 (No Linq avaible). The file can be realy great (up to 1.000.000.000 x 1.000.000.000 lines with up to 100 columns. I dont know how "great" it is but i would say ... great). So i cant read it into the memory. I try to avoid implementing my own file-based-sort-algorithm. Commented May 12, 2014 at 8:19

1 Answer 1

2

Use windows Power-Shell. Command you are interested in is:

Import-CSV [your file] | Sort-Object [column header].

The result will be sorted by the "column header". For more elaborated sort use:

Import-CSV [your file] | Sort-Object [first sort header] [second sort header].

For example, these are my data:

A,B,C,D 1000,1,a,2 99,2,bs,3 1000,3,g,4 66,2,a,3 20,16,3,b 1000,7,c,4 99,1,lz,4

This command: Import-CSV .\test.csv | Sort-Object C will give following result:

A B C D - - - - 20 16 3 b 66 2 a 3 1000 1 a 2 99 2 bs 3 1000 7 c 4 1000 3 g 4 99 1 lz 4

Here is a link that explains it in more details:

http://blogs.technet.com/b/heyscriptingguy/archive/2008/02/12/how-can-i-use-windows-powershell-to-sort-a-csv-file.aspx

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

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.