pipe-cd / pipe-cd/pipecd

sortFunc never returns 0, so sorting reorders items with equal values

Open Beginner friendly
#7,171 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
1.4k
Forks
364
Avg merge
1d 4h
Merged PRs (30d)
84

Description

What happened:

sortFunc in web/src/utils/common.ts never returns 0. When the two values
are equal, a > b is false, so it falls through to the -1 branch:

export const sortFunc = (
  a: string | number,
  b: string | number,
  direction: "ASC" | "DESC" = "ASC"
): number => {
  if (direction === "ASC") return a > b ? 1 : -1;   // equal -> -1
  return a > b ? -1 : 1;                            // equal -> 1
};

So for identical inputs the comparator reports both a < b and b < a.
Array.prototype.sort is only stable when the comparator returns 0 for equal
elements, so equal elements get shuffled instead of keeping their original
order.

What you expected to happen:

Equal values compare as equal (return 0) and keep their relative order.

How to reproduce it:

Sorting a list where several entries share a name:

const items = [
  { name: "api", id: 1 }, { name: "api", id: 2 }, { name: "api", id: 3 },
  { name: "api", id: 4 }, { name: "api", id: 5 }, { name: "api", id: 6 },
  { name: "web", id: 7 }, { name: "api", id: 8 }, { name: "api", id: 9 },
  { name: "api", id: 10 }, { name: "api", id: 11 }, { name: "api", id: 12 },
];
items.sort((a, b) => sortFunc(a.name, b.name)).map(i => i.id);
actual:   12,11,10,9,8,6,5,4,3,2,1,7
expected:  1,2,3,4,5,6,8,9,10,11,12,7

The equal-named entries come out fully reversed. The exact result also depends
on list length, since V8 switches sorting strategy for larger arrays, so short
lists can look fine while longer ones don't.

Where it shows up:

sortFunc sorts the piped/application lists in the four application forms
(application-form-v0, application-form-v1, application-form-manual-v0,
application-form-manual-v1) and in encrypt-secret-drawer, where duplicate
names are normal. sortDateFunc calls sortFunc, so it has the same problem
for equal timestamps.

web/src/utils/common.ts currently has no test file, which is probably why
this went unnoticed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in web/src/utils/common.ts by reading sortFunc and sortDateFunc, then inspect their usages in the application forms and encrypt-secret-drawer. Add focused tests for equal values in ascending and descending sorts, including equal timestamps, and confirm the original order is preserved while existing ordering still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend, testing
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.