I have a .csv file of data which has the following format:

int,string,float,float,float,float, where the string is always 3 characters.

I want to read the file in and sort column 5 relative to column 1.

i.e.

1,b,1.11,2.22,3.33,200.00
3,c,1.11,2.22,3.33,100.00

becomes

3,c,1.11,2.22,3.33,100.00
1,b,1.11,2.22,3.33,200.00

etc.

I'm a novice programmer and I don't quite understand structures that well. I've read that passing poiinters to a structure will work but I'm not sure how to do it.

I want to read the file in and sort column 5 relative to column 1.

I'm not sure I understand what this sentence means as it pertains to your example below:

i.e.

1,b,1.11,2.22,3.33,200.00
3,c,1.11,2.22,3.33,100.00

becomes

3,c,1.11,2.22,3.33,100.00
1,b,1.11,2.22,3.33,200.00

etc.

You have six columns, not 5, and you appear to have also changed column 2's and column 6's order too. Columns 3 through 5 are the same so I can't tell.

Do you mean that you want to treat each row as a single entity, then order the rows based on their first entry (the integer)?

If so, I think you could design a struct containing an int, a char, and 4 floats. You could then write a comparison function like "lessThan" that compares two structs. That comparison function would extract the the integers from each struct and decide whether the first struct was smaller than the second based on that integer value.

Something like this (assuming the name of the struct is "csv") wouldn't work:

csv a, b;
// code
if (a < b)
{
     //code
}

since C++ wouldn't know how to compare a and b. But this would work (after you wrote function lessThan):

csv a, b;
// code
if (lessThan (a, b))
{
     //code
}

since C++ wouldn't know how to compare a and b. But this would work (after you wrote function lessThan):

csv a, b;
// code
if (lessThan (a, b))
{
     //code
}

or, simply compare the relevant members directly

if( a.numeric_member_n < b.numeric_member_n )

Remember that in doing the exchange swap, you don't need to use pointers. You can directly assign a struct object's content to another like struct, as long as they don't contain any dynamically allocated members (which in your example you shouldn't)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.