1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Manuel Imperiale 7834cc48b3
MF-1362 - Sort Things and Channels connections by name (#1363)
* MF-1362 - Sort Things and Channels connections by name

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix reviews

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Remove test

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix reviews

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add order and direction tests

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
2021-02-17 16:21:40 +01:00

64 lines
1.4 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package mocks
import (
"fmt"
"sort"
"github.com/mainflux/mainflux/things"
)
// Since mocks will store data in map, and they need to resemble the real
// identifiers as much as possible, a key will be created as combination of
// owner and their own identifiers. This will allow searching either by
// prefix or suffix.
func key(owner string, id string) string {
return fmt.Sprintf("%s-%s", owner, id)
}
func sortThings(pm things.PageMetadata, ths []things.Thing) []things.Thing {
switch pm.Order {
case "name":
if pm.Dir == "asc" {
sort.SliceStable(ths, func(i, j int) bool {
return ths[i].Name < ths[j].Name
})
}
if pm.Dir == "desc" {
sort.SliceStable(ths, func(i, j int) bool {
return ths[i].Name > ths[j].Name
})
}
default:
sort.SliceStable(ths, func(i, j int) bool {
return ths[i].ID < ths[j].ID
})
}
return ths
}
func sortChannels(pm things.PageMetadata, chs []things.Channel) []things.Channel {
switch pm.Order {
case "name":
if pm.Dir == "asc" {
sort.SliceStable(chs, func(i, j int) bool {
return chs[i].Name < chs[j].Name
})
}
if pm.Dir == "desc" {
sort.SliceStable(chs, func(i, j int) bool {
return chs[i].Name > chs[j].Name
})
}
default:
sort.SliceStable(chs, func(i, j int) bool {
return chs[i].ID < chs[j].ID
})
}
return chs
}