I have a number of very long arrays. No run-time sort is possible. It is also time consuming to sort them manually. Moreover, new elements can be added in any order later, so I would like to sort them by value using C preprocessor or maybe there is any compilers flag (GCC)?
For example:
sometype S[] = {
{somevals, "BOB", someothervals},
{somevals, "ALICE", someothervals},
{somevals, "TIM", someothervals},
}
must be sorted so:
sometype S[] = {
{somevals, "ALICE", someothervals},
{somevals, "BOB", someothervals},
{somevals, "TIM", someothervals},
}
Ok, here is my solution:
sort -b -i --key=2 tobesorted.c
Actually, it would be nice to have some possibility to call "sort" directly from the preprocessor (I had a hope that at least GCC somehow support such features, but it seems that it doesn't).
There are many ways by which the array can be sorted in ascending order, like: Selection Sort. Binary Sort. Merge Sort.
The formula =SORT(A3:B17) uses the default "sort by" and "sort order" settings; thus, the list is sorted in alphabetical order. The syntax for the new SORT function is =SORT(array, [sort_index], [sort_order], [by_column]). The first argument identifies the array to be sorted. All the other arguments are optional.
In programming language, sorting is a basic function which is applied to data to arrange these data is ascending or descending data. In C++ program, there is a function std::sort() for sorting the array.
Do this.
Put your giant array in a file.
Sort the file with the built-in sort
Write a small program to create C code from the file. A C program that writes C programs is fine. You can use Python and some of it's cool template packages to make this job simpler.
Compile the remaining program consisting of the sorted file transformed into C code plus the rest of your program.
No, it is not possible. You cannot do string operations (other than concatenation) with the preprocessor. And you can't compare strings with template metaprograming, either.
[edit] What you could do is put your datastructure in a file that is meant to be preprocessed by an external build script (e.g. the unix "sort" utility), and then modify your makefile/project so that at build time, you generate a C file with the (sorted) initialized arrays
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With