I'm trying to make a sorting algortihm for an array of objects, which I want to be able to sort according to a variable key.
Example of such an object would be:
{"address": "someAddress", "coords": {"lat": someLat, "lon": someLon}}
As you can see there's an object within the object, containing latitude and longitude.
If it was just sorting on address I wanted to do I could do
key = "address";
arr[index][key];
and all would be well.
But I want to be able to sort on address AND latitude/longitude.
But in order to sort on those later ones I would need a second key and that's perfectly fine as well:
key1 = "coords";
key2 = "latitude";
arr[index][key1][key2];
No problem there, except that now I can't sort on address anymore because there's no subkey at address.
I've tried many possibilities already (1 key with "coords.latitude", "coords[latitude]" or the 2 keys and setting the second one to null,...) and tried to google it, but all I can find is the simple 1 variable key. So I assume it's just not possible.
But before I start changing my objects and all the previous code bases on it (or changing the sort algorithm, but that seems the least sensible way to deal with it), I thought why not ask the good folks over at stackoverflow to see if maybe they know a way...
Any help would be greatly appreciated.