aboutsummaryrefslogtreecommitdiff
path: root/lib/sort/sort.go
diff options
context:
space:
mode:
authorJeffas <dev@jeffas.io>2019-10-02 17:16:15 +0100
committerDrew DeVault <sir@cmpwn.com>2019-10-09 19:46:43 -0400
commitd3379dd7f0d2e5caffeda4a97fc72649421cccca (patch)
tree4ee6a1bfb24636cc511eb6e528817eeb46faf258 /lib/sort/sort.go
parent1339faf7881f33762c6e0a4915404e362fc51de1 (diff)
Preserve sorting order in search results
This ensures that the search results follow the order of the current sort so that cycling throught the results proceeds in displayed order.
Diffstat (limited to 'lib/sort/sort.go')
-rw-r--r--lib/sort/sort.go16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/sort/sort.go b/lib/sort/sort.go
index 89c36a9..840f77e 100644
--- a/lib/sort/sort.go
+++ b/lib/sort/sort.go
@@ -3,6 +3,7 @@ package sort
import (
"errors"
"fmt"
+ "sort"
"strings"
"git.sr.ht/~sircmpwn/aerc/worker/types"
@@ -54,3 +55,18 @@ func parseSortField(arg string) (types.SortField, error) {
return types.SortArrival, fmt.Errorf("%v is not a valid sort criterion", arg)
}
}
+
+// Sorts toSort by sortBy so that toSort becomes a permutation following the
+// order of sortBy.
+// toSort should be a subset of sortBy
+func SortBy(toSort []uint32, sortBy []uint32) {
+ // build a map from sortBy
+ uidMap := make(map[uint32]int)
+ for i, uid := range sortBy {
+ uidMap[uid] = i
+ }
+ // sortslice of toSort with less function of indexing the map sortBy
+ sort.Slice(toSort, func(i, j int) bool {
+ return uidMap[toSort[i]] < uidMap[toSort[j]]
+ })
+}