Click here to Skip to main content
15,894,343 members
Home / Discussions / C#
   

C#

 
AnswerRe: Sorting Pin
Luc Pattyn30-Jan-10 11:30
sitebuilderLuc Pattyn30-Jan-10 11:30 
GeneralRe: Sorting Pin
BobInNJ30-Jan-10 12:06
BobInNJ30-Jan-10 12:06 
GeneralRe: Sorting Pin
Luc Pattyn30-Jan-10 12:14
sitebuilderLuc Pattyn30-Jan-10 12:14 
GeneralRe: Sorting Pin
Gideon Engelberth30-Jan-10 19:15
Gideon Engelberth30-Jan-10 19:15 
GeneralRe: Sorting Pin
Luc Pattyn1-Feb-10 3:32
sitebuilderLuc Pattyn1-Feb-10 3:32 
GeneralRe: Sorting Pin
Gerry Schmitz30-Jan-10 14:43
mveGerry Schmitz30-Jan-10 14:43 
GeneralRe: Sorting [modified] Pin
PIEBALDconsult30-Jan-10 17:09
mvePIEBALDconsult30-Jan-10 17:09 
GeneralRe: Sorting [modified] Pin
Gideon Engelberth30-Jan-10 19:18
Gideon Engelberth30-Jan-10 19:18 
Since you did not specify, I assumed that your file was some sort of CSV where each line is a row and the key field is the first piece in the line. If not, the idea should be enough to get you started. If you can hold the keys all in memory along with the other record keeping data, you could do it this way. Otherwise, you will need to do a Luc suggested in the first reply and split into smaller files, sort the smaller, and merge.

This is how I would implement Luc's last suggestion:

C#
//Read the file in and get just the key field along with
//some tracking information to find the line later.
//Getting the lines one at a time instead of all at once like ReadAllLines
//is an exercise left to the reader.
IEnumerable<string> fileEnumer = System.IO.File.ReadAllLines("input.txt");
var rows = (from line in fileEnumer
            select new LineInfo(float.Parse(line.Split(',')[0]), line.Length))
           .ToList();
//Calculate the starting position of each line in the file
long curPos = 0;
foreach (var lineInfo in rows)
{
    lineInfo.LineStart = curPos;
    curPos += lineInfo.LineLength;
}
//Sort the lines by Id
rows.Sort((left, right) => left.Id.CompareTo(right.Id));
//Copy the lines into a new file
using (var inStream = new System.IO.FileStream("input.txt", 
                                               System.IO.FileMode.Open, 
                                               System.IO.FileAccess.Read, 
                                               System.IO.FileShare.None))
{
    using (var reader = new System.IO.StreamReader(inStream))
    {
        using (var output = new System.IO.StreamWriter("output.txt"))
        {
            foreach (var lineInfo in rows)
            {
                inStream.Seek(lineInfo.LineStart, System.IO.SeekOrigin.Begin);
                output.WriteLine(reader.ReadLine());
            }
        }
    }
}

public class LineInfo
{
    public LineInfo(float id, int lineLength)
    {
        this.Id = id;
        this.LineLength = lineLength;
    }
    public float Id { get; private set; }
    public int LineLength { get; private set; }
    public long LineStart { get; set; }
}


EDIT: After further review, if you made a SelectWithPrevious extension method, you could make everything above the copying portion a bit more LINQish and lose the LineInfo class. Note that there is of course no error handling anywhere in any of this code.

C#
public static IEnumerable<TResult> 
    SelectWithPrevious<TSource, TResult>(this IEnumerable<TSource> source,
                                         Func<TSource, TResult, TResult> selector,
                                         TResult firstPrevious)
{
    foreach (var item in source)
    {
        firstPrevious = selector(item, firstPrevious);
        yield return firstPrevious;
    }
}

IEnumerable<string> fileEnumer = null; /* = get lines one at a time */ ;
var rows = from row in fileEnumer
           .SelectWithPrevious((line, previous) => new {Id = float.Parse(line.Split(',')[0]), 
                                                        LineLength = line.Length, 
                                                        LineStart = previous.LineStart + previous.LineLength}, 
                               new {Id = 0.0F, LineLength = 0, LineStart = 0})
           orderby row.Id
           select row;


modified on Sunday, January 31, 2010 1:02 PM

GeneralRe: Sorting Pin
Luc Pattyn31-Jan-10 1:58
sitebuilderLuc Pattyn31-Jan-10 1:58 
GeneralRe: Sorting Pin
Gideon Engelberth31-Jan-10 6:46
Gideon Engelberth31-Jan-10 6:46 
GeneralRe: Sorting Pin
Luc Pattyn31-Jan-10 14:14
sitebuilderLuc Pattyn31-Jan-10 14:14 
GeneralRe: Sorting Pin
Garth J Lancaster31-Jan-10 0:29
professionalGarth J Lancaster31-Jan-10 0:29 
GeneralRe: Sorting Pin
BobInNJ31-Jan-10 6:27
BobInNJ31-Jan-10 6:27 
QuestionMonitoring Folder Access Pin
Yekoor30-Jan-10 8:48
Yekoor30-Jan-10 8:48 
AnswerRe: Monitoring Folder Access [modified] Pin
#realJSOP30-Jan-10 8:53
mve#realJSOP30-Jan-10 8:53 
AnswerRe: Monitoring Folder Access Pin
Gerry Schmitz30-Jan-10 14:53
mveGerry Schmitz30-Jan-10 14:53 
QuestionC# Group new properties Pin
jojoba201030-Jan-10 6:36
jojoba201030-Jan-10 6:36 
AnswerRe: C# Group new properties Pin
DaveyM6930-Jan-10 6:59
professionalDaveyM6930-Jan-10 6:59 
QuestionRe: C# Group new properties Pin
jojoba201030-Jan-10 7:15
jojoba201030-Jan-10 7:15 
AnswerRe: C# Group new properties Pin
Luc Pattyn30-Jan-10 7:39
sitebuilderLuc Pattyn30-Jan-10 7:39 
AnswerRe: C# Group new properties Pin
DaveyM6930-Jan-10 8:15
professionalDaveyM6930-Jan-10 8:15 
QuestionHTMLDocument Pin
Yulianto.30-Jan-10 6:02
Yulianto.30-Jan-10 6:02 
AnswerRe: HTMLDocument Pin
Luc Pattyn30-Jan-10 6:36
sitebuilderLuc Pattyn30-Jan-10 6:36 
GeneralRe: HTMLDocument Pin
Yulianto.30-Jan-10 17:09
Yulianto.30-Jan-10 17:09 
QuestionError 1 Inconsistent accessibility: parameter type Pin
adam parnell30-Jan-10 3:03
adam parnell30-Jan-10 3:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.