Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Version sorting broken in Searchkit upgrade #381

Open
peterbe opened this issue Mar 23, 2018 · 3 comments
Open

Version sorting broken in Searchkit upgrade #381

peterbe opened this issue Mar 23, 2018 · 3 comments
Assignees

Comments

@peterbe
Copy link
Contributor

peterbe commented Mar 23, 2018

See
screen shot 2018-03-23 at 1 39 22 pm

This merged pull request is probably "guilty".

@peterbe peterbe self-assigned this Mar 23, 2018
@peterbe
Copy link
Contributor Author

peterbe commented Mar 23, 2018

Hey @ssetem can you give me a hint here?
Putting the bucketsTransform back as a prop to RefinementAutosuggest doesn't work. I hoped it would be this easy:

diff --git a/ui/src/App.js b/ui/src/App.js
index a47dabc..4d52cb2 100644
--- a/ui/src/App.js
+++ b/ui/src/App.js
@@ -192,6 +192,45 @@ class ProjectInfo extends React.PureComponent {
   }
 }
 
+const sortVersions = filters => {
+  return filters.sort((a, b) => {
+    const partsA = a.key.split(".");
+    const partsB = b.key.split(".");
+    if (partsA.length < 2 || partsB.length < 2) {
+      // Bogus version, list is last.
+      return 1;
+    }
+    let i = 0;
+    while (partsA[i] === partsB[i] && i <= partsA.length) {
+      // Skip all the parts that are equal.
+      i++;
+    }
+    if (!partsA[i] || !partsB[i]) {
+      // Both versions have the same parts, but one has more parts, eg 56.0 and 56.0.1.
+      return partsB.length - partsA.length;
+    }
+    const subPartRegex = /^(\d+)([a-zA-Z]+)?(\d+)?([a-zA-Z]+)?/; // Eg: 0b12pre
+    const subPartA = partsA[i].match(subPartRegex); // Eg: ["0b1pre", "0", "b", "12", "pre"]
+    const subPartB = partsB[i].match(subPartRegex);
+    if (subPartA[1] !== subPartB[1]) {
+      return parseInt(subPartB[1], 10) - parseInt(subPartA[1], 10);
+    }
+    if (subPartA[2] !== subPartB[2]) {
+      if (subPartA[2] && !subPartB[2]) {
+        return 1;
+      }
+      if (subPartB[2] && !subPartA[2]) {
+        return -1;
+      }
+      return subPartB[2].localeCompare(subPartA[2]);
+    }
+    if (subPartA[3] !== subPartB[3]) {
+      return parseInt(subPartB[3], 10) - parseInt(subPartA[3], 10);
+    }
+    return parseInt(partsB[2], 10) - parseInt(partsA[2], 10);
+  });
+};
+
 class App extends Component {
   render() {
     return (
@@ -243,6 +282,7 @@ class App extends Component {
                   size={20}
                   operator="OR"
                   multi={true}
+                  bucketsTransform={sortVersions}
                 />
                 <RefinementAutosuggest
                   field="target.platform"

What I mean by it not working is that the sortVersions doesn't seem to be called.

What I did do was add orderKey="_term" and orderDirection="desc" back in as props and now the list is ordered but I admit I don't know what the difference is between this pure "string based sort" compared to that bespoke sortVersions (which I didn't write). However, I can guess that we want: "60.0b10, 60.0b9, 60.0b8, ..., 60.0b1" not "60.0b9, 60.0b8, ..., 60.0b10, 60.0b1". (since the second number isn't zero-led)

It was my mistake to not notice that some of the custom options were lost such as the sort on certain fields like platform in this PR. Most are easy to put back in but the version one I can't figure out.

@peterbe
Copy link
Contributor Author

peterbe commented Mar 23, 2018

@peterbe
Copy link
Contributor Author

peterbe commented Apr 20, 2018

This doesn't entirely fix it but it makes it much better #419

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant