Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to sort an array of char*'s ? C++

I've got an array of char* in a file. The company I work for stores data in flat files.. Sometimes the data is sorted, but sometimes it's not. I'd like to sort the data in the files.

Now I could write the code to do this, from scratch. Is there an easier way?

Of course an in-place sort would be the best option. I'm working on large files and have little RAM. But I'll consider all options.

All strings are the same length.

This is some sample data:

the data is of fixed length
the Data is of fixed length
thIS data is of fixed lengt

This would represent three records of length 28. The app knows the length. Each record ends with CRLF (\r\n), though it shouldn't matter for this sort.

like image 585
baash05 Avatar asked Nov 28 '22 05:11

baash05


1 Answers

template<size_t length> int less(const char* left, const char* right) {
    return memcmp(left, right, length) < 0;
}

std::sort(array, array + array_length, less<buffer_length>);
like image 84
Leon Timmermans Avatar answered Dec 05 '22 10:12

Leon Timmermans