sequency icon indicating copy to clipboard operation
sequency copied to clipboard

Support key param for sortBy and sortByDescending

Open couzic opened this issue 2 years ago • 1 comments

Currently, it is possible to sort using the key of an item, but only through the Comparator and ComparatorFactory interfaces, for example: https://github.com/winterbe/sequency/blob/7be941df1dce7c5ad50df60c1ddec72b46a54b54/test/sorted.test.ts#L67 https://github.com/winterbe/sequency/blob/7be941df1dce7c5ad50df60c1ddec72b46a54b54/test/sorted.test.ts#L89 https://github.com/winterbe/sequency/blob/7be941df1dce7c5ad50df60c1ddec72b46a54b54/test/sorted.test.ts#L108 https://github.com/winterbe/sequency/blob/7be941df1dce7c5ad50df60c1ddec72b46a54b54/test/sorted.test.ts#L135

The goal of this PR is to enable sorting by key directly on the Sequence type, like:

        sequenceOf(a4, a1, a3, a23)
            .sortedBy("a")
        sequenceOf(a4, a1, a3, a23)
            .sortedByDescending("a")

Also, this PR exports the Comparator and ComparatorFactory types, so that they are added to the docs (which suppresses a couple warnings when generating them)

couzic avatar Mar 29 '22 18:03 couzic

Hello @winterbe, I've been willing to do this for a long time. With this PR, the sorting functions should finally be consistent.

couzic avatar Mar 29 '22 18:03 couzic