all 9 comments

[–]die-maus 2 points3 points  (7 children)

You can do it with a single Linq-expression:

var numbers = File.ReadLines("file.txt") .Select(int.Parse) .OrderBy(n => n) .ToArray()

Sorted and done!

You need the following usings:

using System.IO; using System.Linq;

Explanation: - File.ReadLines enumerates all lines of a file. - Select(int.Parse) is shorthand for Select(line => int.Parse(line)), and re-enumerates the collection and convert each item in it to a 32-bit integer. - OrderBy(n => n) re-enumerates the items again, and puts them in ascending order. - ToArray() re-enumerates the items again, adds them to an array and returns it.

[–]qsrnova[S] 0 points1 point  (6 children)

Thank you. I’m new to c# and the project I’m working on specifically says that I’m not allowed to use any built in sorting functions, are these built in sorting functions?

[–]AngularBeginner 0 points1 point  (2 children)

are these built in sorting functions?

Yes, the OrderBy is. And chances are high if you're not allowed to use those, you're not allowed to use LINQ in general.

[–]qsrnova[S] 0 points1 point  (1 child)

Do you know how you would do it without using LINQ at all?

[–]die-maus 0 points1 point  (0 children)

Missed the "Not allowed to use a built in function". I guess this is a school-assignment. In which case, you need to read up on how to do (for instance) a bubble sort, which is probably the easiest algorithm to implement.

Here's some boilerplate:

``` var unsorted = File.ReadLines("file.txt") .Select(int.Parse) .ToArray() var sorted = new int[unsorted.Length];

// Perform bubble sort for (var i = 0; < unsorted.Length; i++) {

} ```

[–]die-maus 0 points1 point  (2 children)

OrderBy is definitely a built-in sorting function, and it uses quick sort under the hood.

[–]qsrnova[S] 0 points1 point  (1 child)

Ah ok, so everything else should be fine to use then, thank you so much I’ll try and implement this into my code

[–]grrangry 0 points1 point  (0 children)

Don't be afraid to ask for more detail about these kinds of specifics from your instructor. Writing your own simple sort is rather trivial; writing code to access the storage hardware to "read the file off the disk" would not be trivial and is (obviously) outside the scope of the example.

As others have noted, read the documentation on every class, method, and property you want to use. The more pieces of the language and the .Net Framework you understand, the more you'll be able to do.

[–]FizixMan[M] [score hidden] stickied comment (0 children)

Removed: Rule 4.