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

オープン 初心者向け
#7,171 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@iamrishu11 がすでに取り組んでいます。

2026年9月24日 から。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript
領域
frontend, testing

調査の方向性

web/src/utils/common.ts で sortFunc と sortDateFunc を読むことから始め、次に application forms と encrypt-secret-drawer での使用箇所を確認します。昇順および降順のソートで値が等しい場合(タイムスタンプが等しい場合を含む)を対象としたテストを追加し、既存の並び順を維持しながら既存のソートも引き続き機能することを確認します。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Go
スター
1.4k
フォーク
391
平均マージ
4日 1時間
マージ済み PR(30日)
58

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pipe-cd/pipecd のほかの issue

pipe-cd/pipecd の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。