As I work with Objects in Javascript, my goal is to sort them by clustering based on their distances from each other. In Java, I have successfully achieved this using a custom distance function and hierarchical clustering.
Unfortunately, I haven't been able to find suitable alternatives in Javascript that allow for defining a custom distance metric. Despite attempting to implement a standard Arrays.sort function, I am facing challenges with the compare function after determining the distance between the Objects.
Traditionally, a sort comparator would look something like this:
function compareFunction(a, b) {
if (a.nr > b.nr) return 1;
if (a.nr < b.nr) return -1;
return 0;
}
However, in my case, the distance between 'a' and 'b' is determined by their overlap
function compareFunction(clusterA, clusterB) {
let overlap = 0.0;
if (relativeOverlapBetweenClusters.has(clusterA)) {
const relativeOverlapForClusterA = relativeOverlapBetweenClusters.get(clusterA);
if (relativeOverlapForClusterA.has(clusterB)) {
overlap = relativeOverlapForClusterA.get(clusterB);
}
}
return 1 - overlap;
}
Despite implementing this logic, the sorting isn't working correctly. Additionally, I've noticed that the compareFunction is only being called a limited number of times.