|
Bob,
my very first part of the reply wasn't very accurate. You could:
- create a small class (MyItem) that represents an item from your file;
- fill a List<MyItem> with all items (even when the data itself remains in the file);
- provide an IComparer that knows how to compare two such objects;
- then use List.Sort(IComparer) ;
- and finally generate a new file based on the new sort order.
more on this can be found in here[^].
BTW: I'm not saying this is easier than the split and merge approach I suggested earlier.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
FYI, you do not need an IComparer. There is an overload of List.Sort that takes a delegate (and hence a lambda).
|
|
|
|
|
Thanks for the reminder. I have now updated my article.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Too bad the key is a "floating point number" (ugly and not too practical for sorting).
If it was a character key or if you can convert the float, you can use the "SORT" command from the command line / command prompt.
|
|
|
|
|
That reminds me. I did something similar here a few years ago. As I recall, it was in response to a "Friday Programming Quiz".
The file was rows (CSV) of CP members with member ID first. The task was to devise the smallest sort method.
1,...
10,...
100,...
2,...
etc.
My solution was to pipe the file through a program that left-padded the IDs to (perhaps) six places, then piped the output through SORT.
That sort (snicker) of technique may work for the OP as well.
Edit: Whoops, nope, different situation[^].
modified on Saturday, January 30, 2010 11:31 PM
|
|
|
|
|
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:
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();
long curPos = 0;
foreach (var lineInfo in rows)
{
lineInfo.LineStart = curPos;
curPos += lineInfo.LineLength;
}
rows.Sort((left, right) => left.Id.CompareTo(right.Id));
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.
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; ;
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
|
|
|
|
|
Gideon Engelberth wrote: This is how I would implement Luc's last suggestion
you're very kind, however
Gideon Engelberth wrote: System.IO.File.ReadAllLines("input.txt");
is not what I would do when dealing with a problem whose description started with "I have a large file that ...". We do not really want all data in memory at once, so an explicit ReadLine() loop seems more adequate.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Gideon Engelberth wrote: //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");
I did mention that you would have to do something else. I suppose I should have left that as a /* do stuff here */ instead of typing ReadAllLines.
|
|
|
|
|
Sorry, seems I ignored some comments and went straight to the code.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
BobInNJ wrote: Also, I do not have a database system
look at sqlite - Im sure there's a C# interface for it somewhere - I use it all the time (from c++) for sorting/grouping large amounts of data
'g'
|
|
|
|
|
I would like to thank the group for there responses. The responses were quite educational for me as I am still learning C#.
I plan to solve the problem by using the sort command that comes with the package cygwin. As it turns out, the key is always in the range of 0 to 1 so I can treat it as a string.
Bob
|
|
|
|
|
Hi,
I'd like to maintain of list of frequently used folders, something similar to the list of frequently used programs in Start Menu. Is there any way to find get some kind of notification when a user opens a folder using Windows Explorer?
|
|
|
|
|
You may be able to get that info through shell extensions, but more likely you'll have to find out what even is triggered within explorer (using Spy++), and then somehow hooking that event in your own app.
EDIT ===============
There is the FileSystemWatcher object, but I don't think that will let you know when someone is merely browsing a folder...
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
modified on Saturday, January 30, 2010 3:00 PM
|
|
|
|
|
It won't track simple "reads / opens", but you can use the FileSystemWatcher class to monitor file and folder changes, creates and deletes.
|
|
|
|
|
Hi World of CodeProject!
I am writing a userControl(in C# coding) and I added some new Properties to it, Now my question is that :
How can I group this properties ,(Same as .Net that group it properties for each control)
|
|
|
|
|
If you mean grouping them in the property grid then you can use the Component[^] attribute
|
|
|
|
|
No I mean like Size it has to properties in it --->width,Height
|
|
|
|
|
the Size property is a struct, i.e. a composite type. That's what causes its members to be grouped.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
This[^] isn't the best article I've ever done (I should really revisit it and improve it!) but it will show you how it's done.
|
|
|
|
|
How can I get HTMLDocument of an URL? I need to get HTMLDocument from a bulk url. I think I can't use WebBrowser because it needs to wait until the document is competed.
|
|
|
|
|
you can use WebBrowser.Document, which is available when the document is complete, hence look for the WebBrowser.DocumentCompleted event.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
I know the webbrowser. I'm looking for another way, maybe download it as a string and convert it into HTML document?
|
|
|
|
|
I want to build something like this
http://www.codeproject.com/KB/aspnet/ChatApplication.aspx
However when using the example I try to build the chat app and get the error
Error 1 Inconsistent accessibility: parameter type 'ChatClient.IPAddressInfoEventArgs' is less accessible than delegate 'ChatClient.Configurator.IPChangeHandler' \EasyTalk\ChatClient\ChatClient\Configurator.cs 15 30 ChatClient
Any advice or examples of how to build a simple chat application using a web service.
Every google search presents many examples with very little explanation.
thanks for any advice
|
|
|
|
|
The compiler error means a parameter has a type that is less public than the method/delegate referring to it. Making the type (IPAddressInfoEventArgs) public (or the method/delegate not public) should solve that.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Hi,
Maybe it's an unusual request, but I need a practise (just defining a practise not any source code) that I use silverlight with WCF and WWF, for learning them better.
Best wishes
|
|
|
|