From 2493e581aebc90c5e3b1d60c9f0ea533e0973cd8 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 1 Jan 2019 14:49:30 +0100 Subject: [PATCH] Implement support for sorting order --- parser/parser.go | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index 4359b7b..c25e5a4 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -79,38 +79,59 @@ func (queue MailQ) Sort(config qEntrySortConfig) { } sort.Slice(queue.Entries, func(a int, b int) bool { for _, sortBy := range config.attributes { + var cmp bool + var skip bool + // Not sure if these are necessary yet + cmp = false + skip = false switch sortBy.attribute { case "Id": if queue.Entries[a].Id != queue.Entries[b].Id { - return queue.Entries[a].Id < queue.Entries[b].Id + skip = true } + cmp = queue.Entries[a].Id < queue.Entries[b].Id case "Status": if queue.Entries[a].Status != queue.Entries[b].Status { - return queue.Entries[a].Status < queue.Entries[b].Status + skip = true } + cmp = queue.Entries[a].Status < queue.Entries[b].Status case "Date": if !queue.Entries[a].Date.Equal(queue.Entries[b].Date) { - return queue.Entries[a].Date.Before(queue.Entries[b].Date) + skip = true } + cmp = queue.Entries[a].Date.Before(queue.Entries[b].Date) case "Size": if queue.Entries[a].Size != queue.Entries[b].Size { - return queue.Entries[a].Size < queue.Entries[b].Size + skip = true } + cmp = queue.Entries[a].Size < queue.Entries[b].Size case "Sender": if queue.Entries[a].Sender != queue.Entries[b].Sender { - return queue.Entries[a].Sender < queue.Entries[b].Sender + skip = true } + cmp = queue.Entries[a].Sender < queue.Entries[b].Sender case "Recipients": if len(queue.Entries[a].Recipients) != len(queue.Entries[b].Recipients) { - return len(queue.Entries[a].Recipients) < len(queue.Entries[b].Recipients) + skip = true } + cmp = len(queue.Entries[a].Recipients) < len(queue.Entries[b].Recipients) case "Reason": if queue.Entries[a].Reason != queue.Entries[b].Reason { - return queue.Entries[a].Reason < queue.Entries[b].Reason + skip = true } + cmp = queue.Entries[a].Reason < queue.Entries[b].Reason default: // TODO: Handle this error case? } + if skip == true { + continue + } else { + if sortBy.order == "ASC" { + return cmp + } else if sortBy.order == "DESC" { + return !cmp + } + } } return false })