multiple sorting of an array
i search a possibility to sort an array with more than one key (like an ORDER BY X, Y in SQL). I have searched the whole internet after this information, but without any help ...
I have a random-file, with more than 200.000 records. The quicksort-routine for one key is very fast and no problem. But i need more: I have 3 values
And now i want to sort the LAT AND!!! the LONG values tougether, because some LAT values are equal.
Hope i found here a answer of my question ... and a solution of my project (which is stopped since over a week, due this problem )
I would imagine that you would have to create your own sort routine.
The easiest solution would be to use a database for this instead of a flat file.
yes, i know! I know that a database handles this correctly and i know how i can do this. My problem is, that iīm working on a project, where i canīt use any databases or 3rd party addons to sort this datas. I must do it by myself - but how??
I have made many experiments with different sort-algorithms, but i havenīt found any solution for my problem. I donīt need a complete program code, i only need a idea, how i can realize this problem.
I see no way to solve it by myself ...
Ok, lets brake this down.
Lets look at this from the easiest sorting solution. You already know how to sort on one field easily enough, lets take that knowledge and apply it to multiple fields but, for a twist, do it as only one field.
When you apply the sorting logic to the field, combine all of the ordering fields together into one string, delimited by whatever fashion you want, and send that to the sorting function.
Does that make sense?
In principle I agree with Onslaught but it is probably more ilegant and flexible to use a different function that will get the three (or actually the n) fields and return a single sort sequence value. One such function is what Onslaught is saying i.e. simply concanating the three values.
In pseudo code most sorting algorithms look like something like this
re-order list according to results of comparison
where a,b are the values that are to being compared i.e. in most cases the values of some fields.
There is nothing prohibiting of changing that to the return values of some functions.
i.e. a = valueOf(a1,a2,...an)
b = valueOf(b1,b2,....bn)
valueOf can simply be a string concunacation or even something quite complex e.g. non-linear functions e.t.c
Hope this helps more