From 052127902fd3bf4e95588d071b0d31741b920c0a Mon Sep 17 00:00:00 2001
From: Paco Xu <paco.xu@daocloud.io>
Date: Sat, 5 Nov 2022 00:06:41 +0800
Subject: [PATCH] sort version by semantic (#73)

Signed-off-by: Paco Xu <paco.xu@daocloud.io>

Signed-off-by: Paco Xu <paco.xu@daocloud.io>
---
 corefile-tool/cmd/validversions_test.go |  2 +-
 migration/migrate.go                    | 23 ++++++++++++++++++++++-
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/corefile-tool/cmd/validversions_test.go b/corefile-tool/cmd/validversions_test.go
index a892d0f..04aba01 100644
--- a/corefile-tool/cmd/validversions_test.go
+++ b/corefile-tool/cmd/validversions_test.go
@@ -15,7 +15,7 @@ func TestNewValidVersionsCmd(t *testing.T) {
 		{
 			name: "Works without error",
 			expectedOutput: `The following are valid CoreDNS versions:
-1.1.3, 1.1.4, 1.10.0, 1.2.0, 1.2.1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.2.6, 1.3.0, 1.3.1, 1.4.0, 1.5.0, 1.5.1, 1.5.2, 1.6.0, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.6.5, 1.6.6, 1.6.7, 1.6.9, 1.7.0, 1.7.1, 1.8.0, 1.8.3, 1.8.4, 1.8.5, 1.8.6, 1.8.7, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.9.4
+1.1.3, 1.1.4, 1.2.0, 1.2.1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.2.6, 1.3.0, 1.3.1, 1.4.0, 1.5.0, 1.5.1, 1.5.2, 1.6.0, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.6.5, 1.6.6, 1.6.7, 1.6.9, 1.7.0, 1.7.1, 1.8.0, 1.8.3, 1.8.4, 1.8.5, 1.8.6, 1.8.7, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.9.4, 1.10.0
 `,
 			expectedError: false,
 		},
diff --git a/migration/migrate.go b/migration/migrate.go
index 4ba6375..d0d11a1 100644
--- a/migration/migrate.go
+++ b/migration/migrate.go
@@ -10,6 +10,8 @@ import (
 	"fmt"
 	"regexp"
 	"sort"
+	"strconv"
+	"strings"
 
 	"github.com/coredns/corefile-migration/migration/corefile"
 )
@@ -420,7 +422,26 @@ func ValidVersions() []string {
 	for vStr := range Versions {
 		vStrs = append(vStrs, vStr)
 	}
-	sort.Strings(vStrs)
+	sort.Slice(vStrs, func(i, j int) bool {
+		iSegs := strings.Split(vStrs[i], ".")
+		jSegs := strings.Split(vStrs[j], ".")
+		for k, iSeg := range iSegs {
+			if iSeg == jSegs[k] {
+				continue
+			}
+			iInt, err := strconv.Atoi(iSeg)
+			if err != nil {
+				panic(err)
+			}
+			jInt, err := strconv.Atoi(jSegs[k])
+			if err != nil {
+				panic(err)
+			}
+			return iInt < jInt
+		}
+		return false
+	})
+
 	return vStrs
 }