From 840a3ce73260dd7c2846fd214e8740f433002a96 Mon Sep 17 00:00:00 2001 From: Matt Hook Date: Mon, 2 May 2022 12:37:26 +1200 Subject: [PATCH] switch natural sort lib for a better one (#6862) Switched to better natural sorting package --- api/go.mod | 1 + api/go.sum | 2 + api/http/handler/endpoints/sort.go | 6 +- api/internal/natsort/natsort.go | 126 ----------------------------- 4 files changed, 6 insertions(+), 129 deletions(-) delete mode 100644 api/internal/natsort/natsort.go diff --git a/api/go.mod b/api/go.mod index 69e0a97d3..ab2e7168b 100644 --- a/api/go.mod +++ b/api/go.mod @@ -12,6 +12,7 @@ require ( github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9 github.com/docker/cli v20.10.9+incompatible github.com/docker/docker v20.10.9+incompatible + github.com/fvbommel/sortorder v1.0.2 github.com/fxamacker/cbor/v2 v2.3.0 github.com/g07cha/defender v0.0.0-20180505193036-5665c627c814 github.com/go-git/go-git/v5 v5.3.0 diff --git a/api/go.sum b/api/go.sum index 86bfc17b2..ce36b6bbc 100644 --- a/api/go.sum +++ b/api/go.sum @@ -376,6 +376,8 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4 github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa/go.mod h1:KnogPXtdwXqoenmZCw6S+25EAm2MkxbG0deNDu4cbSA= +github.com/fvbommel/sortorder v1.0.2 h1:mV4o8B2hKboCdkJm+a7uX/SIpZob4JzUpc5GGnM45eo= +github.com/fvbommel/sortorder v1.0.2/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0= github.com/fxamacker/cbor/v2 v2.3.0 h1:aM45YGMctNakddNNAezPxDUpv38j44Abh+hifNuqXik= github.com/fxamacker/cbor/v2 v2.3.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= github.com/g07cha/defender v0.0.0-20180505193036-5665c627c814 h1:gWvniJ4GbFfkf700kykAImbLiEMU0Q3QN9hQ26Js1pU= diff --git a/api/http/handler/endpoints/sort.go b/api/http/handler/endpoints/sort.go index 70d4f9d30..169188286 100644 --- a/api/http/handler/endpoints/sort.go +++ b/api/http/handler/endpoints/sort.go @@ -3,8 +3,8 @@ package endpoints import ( "strings" + "github.com/fvbommel/sortorder" portainer "github.com/portainer/portainer/api" - "github.com/portainer/portainer/api/internal/natsort" ) type EndpointsByName []portainer.Endpoint @@ -18,7 +18,7 @@ func (e EndpointsByName) Swap(i, j int) { } func (e EndpointsByName) Less(i, j int) bool { - return natsort.Compare(strings.ToLower(e[i].Name), strings.ToLower(e[j].Name)) + return sortorder.NaturalLess(strings.ToLower(e[i].Name), strings.ToLower(e[j].Name)) } type EndpointsByGroup []portainer.Endpoint @@ -39,5 +39,5 @@ func (e EndpointsByGroup) Less(i, j int) bool { groupA := endpointGroupNames[e[i].GroupID] groupB := endpointGroupNames[e[j].GroupID] - return natsort.Compare(strings.ToLower(groupA), strings.ToLower(groupB)) + return sortorder.NaturalLess(strings.ToLower(groupA), strings.ToLower(groupB)) } diff --git a/api/internal/natsort/natsort.go b/api/internal/natsort/natsort.go deleted file mode 100644 index 503191924..000000000 --- a/api/internal/natsort/natsort.go +++ /dev/null @@ -1,126 +0,0 @@ -// Package natsort implements natural strings sorting - -// An extension of the following package found here: -// https://github.com/facette/natsort -// Our extension adds ReverseSort -// -// Original 3-Clause BSD License below: -// Copyright (c) 2015, Vincent Batoufflet and Marc Falzon -// All rights reserved. - -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions -// are met: - -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. - -// * Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in the -// documentation and/or other materials provided with the distribution. - -// * Neither the name of the authors nor the names of its contributors -// may be used to endorse or promote products derived from this software -// without specific prior written permission. - -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -// POSSIBILITY OF SUCH DAMAGE. - -package natsort - -import ( - "regexp" - "sort" - "strconv" -) - -type natsort []string - -func (s natsort) Len() int { - return len(s) -} - -func (s natsort) Less(a, b int) bool { - return Compare(s[a], s[b]) -} - -func (s natsort) Swap(a, b int) { - s[a], s[b] = s[b], s[a] -} - -var chunkifyRegexp = regexp.MustCompile(`(\d+|\D+)`) - -func chunkify(s string) []string { - return chunkifyRegexp.FindAllString(s, -1) -} - -// Sort sorts a list of strings in a natural order -func Sort(l []string) { - sort.Sort(natsort(l)) -} - -// ReverseSort sorts a list of strings in a natural decending order -func ReverseSort(l []string) { - sort.Sort(sort.Reverse(natsort(l))) -} - -// compare returns true if the first string < second (natsort order) e.g. 1.1.1 < 1.11 -func Compare(a, b string) bool { - chunksA := chunkify(a) - chunksB := chunkify(b) - - nChunksA := len(chunksA) - nChunksB := len(chunksB) - - for i := range chunksA { - if i >= nChunksB { - return false - } - - aInt, aErr := strconv.Atoi(chunksA[i]) - bInt, bErr := strconv.Atoi(chunksB[i]) - - // If both chunks are numeric, compare them as integers - if aErr == nil && bErr == nil { - if aInt == bInt { - if i == nChunksA-1 { - // We reached the last chunk of A, thus B is greater than A - return true - } else if i == nChunksB-1 { - // We reached the last chunk of B, thus A is greater than B - return false - } - - continue - } - - return aInt < bInt - } - - // So far both strings are equal, continue to next chunk - if chunksA[i] == chunksB[i] { - if i == nChunksA-1 { - // We reached the last chunk of A, thus B is greater than A - return true - } else if i == nChunksB-1 { - // We reached the last chunk of B, thus A is greater than B - return false - } - - continue - } - - return chunksA[i] < chunksB[i] - } - - return false -}