Natural sorting helper
Andrew McDermott
andrew.mcdermott at canonical.com
Wed Jun 22 09:15:53 UTC 2016
I created something similar for handling network device names:
https://github.com/juju/juju/blob/master/network/devicenames.go
I'll also look to see if we can use the generalised version.
On 22 June 2016 at 09:57, roger peppe <rogpeppe at gmail.com> wrote:
> Nice! Could I suggest that the Less implementation is exported
> independently so that it can be used in other sort.Interface
> implementations (for example if we wanted to sort a slice of
> structs by a naturally-sorted key)?
>
> Perhaps call it StringCompareNatural and make it the same
> signature as strings.Compare?
>
>
>
> On 22 June 2016 at 11:35, Menno Smits <menno.smits at canonical.com> wrote:
> > Hi everyone,
> >
> > Earlier this week I needed to be able sort a slice containing unit names
> and
> > machine ids in a way that would make sense to a human. For example, a
> > conventional string sort would order a list of machine ids like this:
> >
> > 0
> > 10
> > 3
> > 3/lxd/1
> > 3/lxd/10
> > 3/lxd/11
> > 3/lxd/2
> > 4
> >
> > when what I really wanted was:
> >
> > 0
> > 3
> > 3/lxd/1
> > 3/lxd/2
> > 3/lxd/10
> > 3/lxd/11
> > 4
> > 10
> >
> > Tim pointed me at something that Anastasia had already done for
> formatting
> > the output from some Juju CLI commands. This was close so I extracted it
> to
> > github.com/juju/utils and generalised it. An in-place sort can be
> performed
> > like this:
> >
> > utils.SortStringsNaturally(someSliceOfStrings)
> >
> > The implementation is here:
> >
> > https://github.com/juju/utils/blob/master/naturalsort.go
> > https://github.com/juju/utils/blob/master/naturalsort_test.go
> >
> > Consider using it if your code needs to sort machine ids, unit names, tag
> > strings, IP addresses and any other slice of strings which contain
> sections
> > of digits.
> >
> > - Menno
> >
> >
> >
> >
> > --
> > Juju-dev mailing list
> > Juju-dev at lists.ubuntu.com
> > Modify settings or unsubscribe at:
> > https://lists.ubuntu.com/mailman/listinfo/juju-dev
> >
>
> --
> Juju-dev mailing list
> Juju-dev at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/juju-dev
>
--
Andrew McDermott <andrew.mcdermott at canonical.com>
Juju Core Sapphire team <http://juju.ubuntu.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/juju-dev/attachments/20160622/b168c8ef/attachment.html>
More information about the Juju-dev
mailing list