diff --git a/.version b/.version index 29c7dec..20fe357 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -VERSION=0.7.4 \ No newline at end of file +VERSION=0.7.5 \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index fe16d51..b1d9c51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,24 +5,21 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [v0.7.3] - 2022-08-30 - +## [v0.7.5] - 2022-09-14 +### Fixed +- Sort by IP [Issue #4](https://github.com/aceberg/WatchYourLAN/issues/4), [Issue #14](https://github.com/aceberg/WatchYourLAN/issues/14) + +## [v0.7.3] - 2022-08-30 ### Added - Workflow to build ARM images - ## [v0.7.2] - 2022-08-26 - ### Added - Theme support in config - ## [v0.7.1] - 2022-08-26 - ### Added - Workaround for correct display of strings with apostrophes - ### Fixed - - [Issue #2](https://github.com/aceberg/WatchYourLAN/issues/2) - [Issue #5](https://github.com/aceberg/WatchYourLAN/issues/5) \ No newline at end of file diff --git a/src/web-sort.go b/src/web-sort.go index eaa9e40..e6e5fa0 100644 --- a/src/web-sort.go +++ b/src/web-sort.go @@ -3,8 +3,41 @@ package main import ( "net/http" "sort" + "bytes" + "net" ) +func sort_by_ips(method string) { + type ipHost struct { + Host Host + Ip net.IP + } + toSort := []ipHost{} + var oneSort ipHost + + for _, oneHost := range AllHosts { + oneSort.Host = oneHost + oneSort.Ip = net.ParseIP(oneHost.Ip) + toSort = append(toSort, oneSort) + } + + switch method { + case "asc": + sort.Slice(toSort, func(i, j int) bool { + return bytes.Compare(toSort[i].Ip, toSort[j].Ip) < 0 + }) + case "desc": + sort.Slice(toSort, func(i, j int) bool { + return bytes.Compare(toSort[i].Ip, toSort[j].Ip) > 0 + }) + } + + AllHosts = []Host{} + for _, oneSort := range toSort { + AllHosts = append(AllHosts, oneSort.Host) + } +} + func sort_hosts(w http.ResponseWriter, r *http.Request) { sort_method := r.FormValue("sort_method") @@ -19,13 +52,9 @@ func sort_hosts(w http.ResponseWriter, r *http.Request) { return AllHosts[i].Name > AllHosts[j].Name }) case "ip-up": - sort.SliceStable(AllHosts, func(i, j int) bool { - return AllHosts[i].Ip < AllHosts[j].Ip - }) + sort_by_ips("asc") case "ip-down": - sort.SliceStable(AllHosts, func(i, j int) bool { - return AllHosts[i].Ip > AllHosts[j].Ip - }) + sort_by_ips("desc") case "date-up": sort.SliceStable(AllHosts, func(i, j int) bool { return AllHosts[i].Date < AllHosts[j].Date