Mix lines in a file in .NET.

I have a file "new.txt", for example,

asdfg
qwerty
zcx
poi

Now I need to shuffle the lines of this text file. How can I do this in C #.?

+3
source share
6 answers
var lines = File.ReadAllLines("test.txt");
var rnd = new Random();
lines = lines.OrderBy(line => rnd.Next()).ToArray();
File.WriteAllLines("test.txt", lines);
+8
source

a not very efficient way: read the file in IEnumerable<string>, do .OrderBy(line => Guid.NewGuid())and write it to a file

eg.

var originalLines = File.ReadAllLines("test.txt");
var shuffledLines = lines.OrderBy(line => Guid.NewGuid()).ToArray();
File.WriteAllLines("test.txt", shuffledLines);
+4
source
+3

MS Excel .

1) MS Excel.

2) (, B1) = rand().

3) B,

4) Edit- > Fill- > Down

5) B .

, .

+1

.

0

List<string>

, , ...

0

Source: https://habr.com/ru/post/1730361/


All Articles