Implement support for sorting order

This commit is contained in:
Jan Philipp Timme 2019-01-01 14:49:30 +01:00
parent f22be8fccf
commit 2493e581ae
1 changed files with 28 additions and 7 deletions

View File

@ -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
})