Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support sorting by multiple values. #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

stephenh
Copy link
Contributor

No description provided.

@stephenh stephenh requested a review from zgavin November 25, 2024 14:56
@@ -39,7 +39,7 @@ export function isDefined<T extends any>(param: T | undefined | null): param is
return param !== null && param !== undefined;
}

export type Comparable = string | number | bigint | Date | Temporal.PlainDate | Temporal.ZonedDateTime;
export type Comparable = string | number | bigint | Date | Temporal.PlainDate | Temporal.ZonedDateTime | undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We check for undefined on the 1st line of compare's impl, so seems like undefined was already allowed as a Comparable

const av = fn(a);
const bv = fn(b);
if (Array.isArray(av) && Array.isArray(bv)) {
for (let i = 0; i < av.length; i++) {
Copy link
Contributor

@zgavin zgavin Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: i think we need to consider if the arrays have different lengths? eg if bv was [1,2,3] and av was [1,2], then bv should sort before, but with this code they'd sort as equal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of finding some fancy TypeScript that would force the lambda to always return the same length-d array; ChatGTP had some suggestions:

type LambdaWithFixedLength<N extends number, T = unknown> = (...args: any[]) => FixedLengthArray<T, N>;

type FixedLengthArray<T, N extends number, R extends unknown[] = []> = R["length"] extends N
  ? R
  : FixedLengthArray<T, N, [T, ...R]>;

But I couldn't get it to work in the time box I had for adding this, so was pretty tempted to be satisfied with the "don't do that" version of the lambda returning different length-d arrays.

I suppose could do a runtime error if they're different lengths. Pushed that up.

Does that sgty?

Copy link
Contributor

@zgavin zgavin Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dunno i feel like different length arrays is a valid use case and not that hard to support? eg, lets say you were doing semantic versioning and to sort you emitted the version as an array of numbers. The arrays might have different lengths but they'd still be meaningful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 maybe I guess; personally I'd assume that different length arrays in the wild would 80% of the time be bugs the programmer didn't know/mean to write. 😢

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you're just envisioning the use case where you'd have like foos.sortBy((foo) => [foo.bar, foo.baz]) and i'm envisioning something more dynamic? I don't think the dynamic case is that unrealistic to use. Also it'd be like a 1 line change:

Suggested change
for (let i = 0; i < av.length; i++) {
const length = Math.max(av.length, bv.length);
for (let i = 0; i < length; i++) {

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

Successfully merging this pull request may close these issues.

2 participants