Click to See Complete Forum and Search --> : file sorting


Lil'Hasher
March 28th, 2003, 03:55 PM
Hi all,

this is actually more of a Unix question .... (maybe).

Within my C++ code i'd like to sort a file based on a particular field. The file has *no field delimiters* but i know the byte length of each field. The only way i can think of doing this effeciently is using a system call to Unix's "sort". However from what i can tell sort doesn't work when sorting by a particular field, unless the file is field delimited or has at least one blank space between fields. My file has neither.

So my question is, is there effecient way to sort this file using either C++ code or System calls to unix commands?
(and again the sorting is on a particular field)

Also, why doesnt unix allow sorting based on a byte position (byte interval)???

thanks as always,
lil hasher

Bob Davis
March 28th, 2003, 04:16 PM
If I'm understanding your problem correctly, I'm assuming that you have a data file that has a number of fields for each data point, with a sequence of data points repeated throughout the file. If this is true, you could do this:

1. Create a class or struct that contains the members of each data field.

2. Parse the data file, reading each data value into an instance of the class you created. Insert each one into a std::vector<yourClass>.

3. Write a predicate that compares objects of your class based on the field you want to sort on.

4. Use std::sort to sort the vector's contents.

5. If necessary, output the objects one by one back into the data file.