[lxc-devel] [lxd/master] Tweak sorting in CLI

stgraber on Github lxc-bot at linuxcontainers.org
Wed Apr 8 01:48:56 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200407/c7346cc3/attachment-0001.bin>
-------------- next part --------------
From e5e1b45192035345cd5cb1470bb86012b73707e3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 7 Apr 2020 21:39:57 -0400
Subject: [PATCH 1/2] lxc: Use natural string sorting
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #7149

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxc/utils.go | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/lxc/utils.go b/lxc/utils.go
index a34c1bb26e..b242660f90 100644
--- a/lxc/utils.go
+++ b/lxc/utils.go
@@ -8,6 +8,7 @@ import (
 	"strings"
 
 	"github.com/pkg/errors"
+	"vbom.ml/util/sortorder"
 
 	lxd "github.com/lxc/lxd/client"
 	"github.com/lxc/lxd/shared/api"
@@ -41,7 +42,7 @@ func (a stringList) Less(i, j int) bool {
 		return true
 	}
 
-	return a[i][x] < a[j][x]
+	return sortorder.NaturalLess(a[i][x], a[j][x])
 }
 
 // Instance name sorting
@@ -64,7 +65,7 @@ func (a byName) Less(i, j int) bool {
 		return true
 	}
 
-	return a[i][0] < a[j][0]
+	return sortorder.NaturalLess(a[i][0], a[j][0])
 }
 
 // Storage volume sorting
@@ -80,7 +81,7 @@ func (a byNameAndType) Swap(i, j int) {
 
 func (a byNameAndType) Less(i, j int) bool {
 	if a[i][0] != a[j][0] {
-		return a[i][0] < a[j][0]
+		return sortorder.NaturalLess(a[i][0], a[j][0])
 	}
 
 	if a[i][1] == "" {
@@ -91,7 +92,7 @@ func (a byNameAndType) Less(i, j int) bool {
 		return true
 	}
 
-	return a[i][1] < a[j][1]
+	return sortorder.NaturalLess(a[i][1], a[j][1])
 }
 
 // Batch operations

From 13546f9cb337813ed1530324089c1208ede091f4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 7 Apr 2020 21:45:46 -0400
Subject: [PATCH 2/2] lxc: Group snapshot and parent
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxc/utils.go | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lxc/utils.go b/lxc/utils.go
index b242660f90..6dcbb9b285 100644
--- a/lxc/utils.go
+++ b/lxc/utils.go
@@ -80,7 +80,11 @@ func (a byNameAndType) Swap(i, j int) {
 }
 
 func (a byNameAndType) Less(i, j int) bool {
-	if a[i][0] != a[j][0] {
+	// Sort snapshot and parent together.
+	iType := strings.Split(a[i][0], " ")[0]
+	jType := strings.Split(a[j][0], " ")[0]
+
+	if iType != jType {
 		return sortorder.NaturalLess(a[i][0], a[j][0])
 	}
 


More information about the lxc-devel mailing list