0

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.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Sven Cazier
  • 397
  • 4
  • 12
  • 2
    perhaps if you showed your actual `"sorting code"`, someone could point out what it is you're doing wrong - because, why would address need a "subkey" to be sortable? makes zero sense – Jaromanda X Mar 14 '19 at 22:12
  • 1
    Maybe you want something like this: [Accessing nested JavaScript objects with string key](https://stackoverflow.com/q/6491463/218196). Or make the sort function accept callbacks that return the values from the object that it should be sorted by. – Felix Kling Mar 14 '19 at 22:14
  • @JaromandaX I don't think you understood what I was trying to accomplish. The sorting algorithm can be any algorithm you want... It's just sorting on value. Problem is getting the right value from the object. I want to be able to use the same function for both sorting on address and sorting on latitude/longitude. And that's right, address doesn't need a "subkey", but latitude/longitude does. And therein lies the problem. – Sven Cazier Mar 15 '19 at 00:47
  • @FelixKling A quick look at that link seems like that could do the trick. Would have to look at it closer first though. – Sven Cazier Mar 15 '19 at 00:48
  • I understand exactly what you wrote in the question ... which has absolutely **no** code that you are having an issue with, so please don't be so condescending ... so rewrite your algorithm to test if `key2` is defined or not - seeing as you still refuse to show the sorting code, that's all I got – Jaromanda X Mar 15 '19 at 00:49
  • @JaromandaX I'm sorry if I sounded condescending, but it is like I said: the problem is not the sorting. It's accessing the data from the object to sort the array on different keys. If I were to compare arr[i][key] < arr[j][key] that would work for address but not for lat and lon. If I were to compare arr[i][key1][key2] < arr[j][key1][key2] that would work for lat and lon but not for address. And I would like to use the same algorithm for both. – Sven Cazier Mar 15 '19 at 18:52
  • @FelixKling This function works wonders. I just tried it with a fiddle of my own: https://jsfiddle.net/g540b8wj/1/ Thank you so much for your reply! – Sven Cazier Mar 15 '19 at 18:55
  • I know the problem isn't the sorting, except that it is, because you state that is the problem ... you can't sort with one key if your sorting code expects two - the problem is the sorting code – Jaromanda X Mar 16 '19 at 01:25

0 Answers0