473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

STL sorting comparison function

Hi,

I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer.

What I try to do is sort the records in a plain-text index file based on certain columns. The index file consists of records and fields within the records. The individual fields are separated by semicolons, the records by newlines. The index file is loaded into memory and the only thing I actually have is a char* to the beginning of the index file.

It is possible to access the individual records of a file by (say) record.getPos() and record.getLen() methods that return the position and the length of the field in that record. A record is simply a row of the index file and is loosely defined in a separate class. I am also able to retrieve the length of a complete record (which is fixed for every index file generated) so I could define new records if I want to.

Now what I would like to do is sort this index file using STL sort() and a vector containing some representation of the records in the index file. Because this index file is very large, I think pointers should be used in one way or another. Does anyone have an idea of how I can write a comparison function? I've been trying to do this, but no success. The problem is that I want the comparison function to loop through a vector of sortfields. The sortfields define the columns to be sorted on.

My idea now is to break the complete index file that is in memory (just a char* to the beginning of the file is all we have, we have to jump through the index file manually by adding the length of the record every time). That means, breaking up the file in either separate char*, record* or records... BEFORE sorting it! I guess pointers would be fastest.

The index file looks approximately like this (fields can be empty, don't worry about the semantics of the file, there are more fields but I left them out for space reasons):
1; 55000; 55000; 193400080001; 1934000800010001; ; ;1
1; 55100; 55000; 193400230001; 1934002300010001; ; ;1
1; 55120; 55000; 193400440001; 1934004400010001; ; ;1

But now: How do I define the comparison function for the STL sort. Suppose we break the file into record*'s we get something like sort(vector.begin(), vector.end(), vector_comparison)

With vector comparison being a sort of functor like

(pseudocode)
class vector_comparison
{
bool operator() (record* A, record* B)
{
if (columnA of recordA < columnA of recordB)
return true;
if (columnB of recordA < columnB of recordB)
return true;
...
else
return false; // the records are equal
}
}
The biggest problem here is that this comparison function will need to access external resources such as a list iterator over the 'columns to be sorted on', which is only made at runtime, the vector of indexfile representation record*/char*/records, which is made at runtime... I cannot write this comparison function because it needs a lot of things which are created at runtime... These are not known at compile time... Maybe I understand something incorrectly about functors and I don't even know if this problem can be solved in this way. I am just trying, it is my last effort before I give up on this problem :)

I hope my question is clear, if you need more information please let me know! I'll check back tomorrow
Nov 13 '06 #1
0 2546

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort...
4
by: rushik | last post by:
Hello all, I am using structure in my program, and my aim is to sort this structure based on some optimized sorting algo. structure is struct data { int account;
4
by: Trent | last post by:
Still have problems with this thing. Seems my results are not matching the "correct" example given. The three sets of numbers below the last 3 columns is suppose to be the number of comparisons...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.