diff --git a/src/chartserver/chart_operator.go b/src/chartserver/chart_operator.go index 4908536c8..29da4539d 100644 --- a/src/chartserver/chart_operator.go +++ b/src/chartserver/chart_operator.go @@ -5,6 +5,8 @@ import ( "encoding/json" "errors" "fmt" + "sort" + "strings" "time" "github.com/Masterminds/semver" @@ -45,6 +47,7 @@ type ChartInfo struct { Name string TotalVersions uint32 `json:"total_versions"` Created time.Time + Updated time.Time Icon string Home string Deprecated bool @@ -135,6 +138,16 @@ func (cho *ChartOperator) GetChartList(content []byte) ([]*ChartInfo, error) { } } + //Sort the chart list by the updated time which is the create time + //of the latest version of the chart. + sort.Slice(chartList, func(i, j int) bool { + if chartList[i].Updated.Equal(chartList[j].Updated) { + return strings.Compare(chartList[i].Name, chartList[j].Name) < 0 + } + + return chartList[i].Updated.After(chartList[j].Updated) + }) + return chartList, nil } diff --git a/src/chartserver/chart_operator_test.go b/src/chartserver/chart_operator_test.go index 5982f1525..24bbe3618 100644 --- a/src/chartserver/chart_operator_test.go +++ b/src/chartserver/chart_operator_test.go @@ -35,15 +35,8 @@ func TestGetChartList(t *testing.T) { t.Fatalf("Length of chart list should be 2, but we got %d now", len(infos)) } - foundHarbor := false - for _, chart := range infos { - if chart.Name == "harbor" { - foundHarbor = true - break - } - } - - if !foundHarbor { - t.Fatal("Expect chart named with 'harbor' but got nothing") + firstInSortedList := infos[0] + if firstInSortedList.Name != "harbor" { + t.Fatalf("Expect the fist item of the sorted list to be 'harbor' but got '%s'", firstInSortedList.Name) } }