I have a program that uses (and must continue to use) an old sorting function implementing qsort. I must also provide the sorting function with proper data to sort data both ascending (if string contains even numbers) or descending (if string contains odd numbers).
The data must be altered to achieve this, the sorting function cannot be altered.
The code is written in C but I have no relevant code snippet for this particular problem.
The real question is:
How do I transform the data so that the output matches the desired output below?
I have the following data (or similar)
String 1 String 2 String 3 String 4 String 5 String 6
EDIT: The data is a number of strings type char **, the number within each string is an int.
The desired output is
String 5 String 3 String 1 String 2 String 4 String 6
Sorting is usually done in a descending fashion matching the input 1:1. I have managed to produce a transformation rendering the following output by prepending 1 or 0 to the numbers following the string.
So the internal data to be sorted looks like this
String 01 String 12 String 03 String 14 String 05 String 16
This produces the following output (transformation is only used in sorting, and is temporary).
String 1 String 3 String 5 String 2 String 4 String 6
You should have a struct which contains data, and value:
like
{"01", 1}Then sort by value and output data,
sorting is not hard if you want to do usual sort:
first sort by value to make list like what you shown. (for values)
now create an empty array of data values (with base array size), start from last item and fill it as bellow:
At last output the values.
I wrote it in c# it’s not very different in c.
you will get: