Native Sort Function

I ran into an issue where I used FQL to shape my returning data the way I wanted, however, I needed to return that data sorted by a value back to the client. I sat and tried to use every method in FQL that I could to achieve this but to no avail. I don’t actually believe sorting with FQL is even possible at this point. I know indexes can be sorted, but I don’t believe that an index can be used for the logic I had to make in my UDF. The only option I had in this case was to return the array of objects back to the client and sort from the client. Obviously this is not ideal because it gets rid of the possibility of pagination. It also goes against the point of using Fauna which is to do most of the backend logic using FQL. A native version of Sort would be great to be able to solve this problem.

Here is the original post that lead me to this conclusion How to sort or rank fuzzy logic Ngrams search by most relevant result

So I was incorrect. It actually is possible to make a sorting algorithm using FQL. I lifted the source code from a package made by ShiftX. It includes helper functions that I also deconstructed. Here is a generic version of the UDF query deconstructed.

Query(
  Lambda(
    ["inputArray"],
     Reduce(
          Lambda(
            ["result", "item"],
            Let(
              {
                length: Count(Var("result")),
                index: If(
                  Equals(0, Var("length")),
                  0,
                  Select(
                    1,
                    Select(
                      0,
                      Filter(
                        Reduce(
                          Lambda(
                            ["acc", "val"],
                            Append(
                              [[Var("val"), Count(Var("acc"))]],
                              Var("acc")
                            )
                          ),
                          [],
                          Var("result")
                        ),
                        Lambda(
                          ["nextItem", "index"],
                          GT(
                            Var("nextItem"),
                            Var("item")
                            )
                          )
                        )
                      ),
                      null
                    ),
                    Var("length")
                  )
                )
              },
              Let(
                {
                  arr: Var("result"),
                  index: Var("index"),
                  item: Var("item")
                },
                If(
                  Equals(-1, Var("index")),
                  Append([Var("item")], Var("arr")),
                  Let(
                    {
                      start: Take(Var("index"), Var("arr")),
                      end: Drop(Var("index"), Var("arr"))
                    },
                    Prepend(Prepend(Var("start"), [Var("item")]), Var("end"))
                  )
                )
              )
            )
          ),
          [],
          Var("inputArray")
        )
  )
)
Sort([8, 2, 4]) // => [2, 4, 8]
Sort(["foo", "bar"]) // => ["bar", "foo"]

I definitely recommend looking into some of the work he’s done and expanding FQL to create more useful functions like this.

2 Likes