Board index » cppbuilder » Function object and std:sort.
Digby Millikan
CBuilder Developer |
Function object and std:sort.2007-12-26 12:22:56 PM cppbuilder74 Dear Sir, Hello, I wish to sort a two dimensional array but was wondering about the function object and sort command; Here is the array declaration; struct stringlist{ AnsiString filename; int str; int points; }stringlist1[11][2000]; 1 to 11 represents the i elements and 1 to 2000 represents the j elements. The value filename is always equal for each ith element. Here is example input data; stringlist1[0][1].filename = "file2"; stringlist1[0][1].str = 2; stringlist1[0][1].points = 8; stringlist1[0][2].filename = "file2"; stringlist1[0][2].str = 1; stringlist1[0][2].points = 11; stringlist1[1][1].filename = "file1"; stringlist1[1][1].str = 2; stringlist1[1][1].points = 8; stringlist1[1][2].filename = "file1"; stringlist1[1][2].str = 1; stringlist1[1][2].points = 11; The i elements and filename values are already in correct order. I wish to only sort by the jth element str values so the output would be; stringlist1[0][1].filename = "file2"; stringlist1[0][1].str = 1; stringlist1[0][1].points = 11; stringlist1[0][2].filename = "file2"; stringlist1[0][2].str = 2; stringlist1[0][2].points = 8; stringlist1[1][1].filename = "file1"; stringlist1[1][1].str = 1; stringlist1[1][1].points = 11; stringlist1[1][2].filename = "file1"; stringlist1[1][2].str = 2; stringlist1[1][2].points = 8; What would the function object and sort command look like? Here is my draft; struct stringlists{ bool operator() (const stringlist& lhs, const stringlist& rhs) const { return (lhs.str < rhs.str); } }; std::sort(stringlist1 , stringlist1 + 2, stringlists()); Is this correct? Thankyou, Regards Digby Millikan. |