From ff77fbf5d96f9250f7478036148deff87ac6ca3f Mon Sep 17 00:00:00 2001 From: Simon Aquino Date: Fri, 13 Jun 2014 11:42:20 +0100 Subject: [PATCH 1/2] Sort PackagesList by name and version --- deb/list.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/deb/list.go b/deb/list.go index b615b5d2..cf46bb40 100644 --- a/deb/list.go +++ b/deb/list.go @@ -300,9 +300,16 @@ func (l *PackageList) Swap(i, j int) { l.packagesIndex[i], l.packagesIndex[j] = l.packagesIndex[j], l.packagesIndex[i] } -// Compare compares two names in lexographical order +// Compare compares two packages by name in lexographical order and version func (l *PackageList) Less(i, j int) bool { - return l.packagesIndex[i].Name < l.packagesIndex[j].Name + iPkg := l.packagesIndex[i] + jPkg := l.packagesIndex[j] + + if iPkg.Name == jPkg.Name { + return CompareVersions(iPkg.Version, jPkg.Version) == -1 + } + + return iPkg.Name < jPkg.Name } // PrepareIndex prepares list for indexing From e19a615641c9c4160066ef4df95fb47af1a0bdfa Mon Sep 17 00:00:00 2001 From: Simon Aquino Date: Fri, 13 Jun 2014 12:06:33 +0100 Subject: [PATCH 2/2] In PackageList, sort the package version from latest to oldest This enables us to return the latest version of a package when no version is specified rather than the oldest. Therefore, we don't need to modify the search algorithm to return the latest version of a package. --- deb/list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deb/list.go b/deb/list.go index cf46bb40..cbe25d5f 100644 --- a/deb/list.go +++ b/deb/list.go @@ -300,13 +300,13 @@ func (l *PackageList) Swap(i, j int) { l.packagesIndex[i], l.packagesIndex[j] = l.packagesIndex[j], l.packagesIndex[i] } -// Compare compares two packages by name in lexographical order and version +// Compare compares two packages by name (lexographical) and version (latest to oldest) func (l *PackageList) Less(i, j int) bool { iPkg := l.packagesIndex[i] jPkg := l.packagesIndex[j] if iPkg.Name == jPkg.Name { - return CompareVersions(iPkg.Version, jPkg.Version) == -1 + return CompareVersions(iPkg.Version, jPkg.Version) == 1 } return iPkg.Name < jPkg.Name