Skip to content

Instantly share code, notes, and snippets.

@robbeman
Last active April 25, 2019 07:27
Show Gist options
  • Save robbeman/3212e35f75a6bd326e3773aff32c687e to your computer and use it in GitHub Desktop.
Save robbeman/3212e35f75a6bd326e3773aff32c687e to your computer and use it in GitHub Desktop.
Simple coordinate sorting
/**
* Sorting markers in rows from left to right. Because geolocation can be pretty
* detailed we can't just sort it as is, we need a math to make it feel natural.
*
* How this works:
* - Prioritise sorting on latitude (y-axis)
* - multiply the difference and round it this will "group" locations in rows.
* - 30 is an arbitrary value that felt good in my case, higher values will make tighter rows.
* - (Using `1` as multiplier you will see the sorting jumps long distances from left to right.)
* - If 0 (same row), sort by longitude, with full precision
* - Depending on your dataset you might still see some jumps
*/
const sortedMakers = markers.sort((a, b) => {
return Math.round(b.lat * 30) - Math.round(a.lat * 30) || a.long - b.long;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment