Reputation: 1275
I have just started programming in Golang and want to sort an array of maps. I have an array of maps. Let's call it example_array.
example_array = [
[
{ Name: "A", Value: 100 },
{ Name: "B", Value: 60 },
{ Name: "C", Value: 170 },
{ Name: "D", Value: 120}
],
[
{ Name: "A", Value: 64 },
{ Name: "B", Value: 90 },
{ Name: "C", Value: 52 },
{ Name: "D", Value: 98}
],
[
{ Name: "A", Value: 154 },
{ Name: "B", Value: 190 },
{ Name: "C", Value: 179 },
{ Name: "D", Value: 67 }
]
]
Now I want to sort this array using value of key "C"
so the example_array should be modified to ->
[
[{Name: "A", Value: 64}, {Name: "B", Value: 90}, {Name: "C", Value: 52}, {Name: "D", Value: 98}],
[{Name: "A", Value: 100}, {Name: "B", Value: 60}, {Name: "C", Value: 170}, {Name: "D", Value: 120}],
[{Name: "A", Value: 154}, {Name: "B", Value: 190}, {Name: "C", Value: 179}, {Name: "D", Value: 67}]
]
If I sort the original array using value of key "D"
, the original array should be modified to ->
[
[{Name: "A", Value: 154}, {Name: "B", Value: 190}, {Name: "C", Value: 179}, {Name: "D", Value: 67}],
[{Name: "A", Value: 64}, {Name: "B", Value: 90}, {Name: "C", Value: 52}, {Name: "D", Value: 98}]
[{Name: "A", Value: 100}, {Name: "B", Value: 60}, {Name: "C", Value: 170}, {Name: "D", Value: 120}]
]
How can I sort these array of maps in Golang. Please help!
Upvotes: 2
Views: 5195
Reputation: 4422
Your data looks like it could be easily represented as a slice of maps of type map[string]int
. Since you didn't provide any Go code in your question, I cannot be sure of the data types, so I will assume it is a slice of maps of type map[string]int
in this answer.
A simple way to sort a slice of maps is to use the sort.Slice
function. From a comment in the first example in the sort
package documentation:
use
sort.Slice
with a custom Less function, which can be provided as a closure. In this case no methods are needed
The Less function needs to satisfy the signature
func(i, j int) bool
Per package documentation (at Interface):
Less reports whether the element with index
i
should sort before the element with indexj
.
Using a closure allows you to reference your data structure in the function body even though it is not part of the parameter list.
Here's a runnable example that sorts a slice of map[string]int
values matching the data in your question:
package main
import(
"fmt"
"sort"
)
func main() {
in := []map[string]int{
{
"A": 100,
"B": 60,
"C": 170,
"D": 120,
},
{
"A": 64,
"B": 90,
"C": 52,
"D": 98,
},
{
"A": 154,
"B": 190,
"C": 179,
"D": 67,
},
}
for k, _ := range in[0] {
sort.Slice(in, func(i, j int) bool { return in[i][k] < in[j][k] })
fmt.Printf("By %s: %v\n", k, in)
}
}
Output:
By A: [map[A:64 B:90 C:52 D:98] map[A:100 B:60 C:170 D:120] map[A:154 B:190 C:179 D:67]]
By B: [map[A:100 B:60 C:170 D:120] map[B:90 C:52 D:98 A:64] map[C:179 D:67 A:154 B:190]]
By C: [map[A:64 B:90 C:52 D:98] map[A:100 B:60 C:170 D:120] map[A:154 B:190 C:179 D:67]]
By D: [map[A:154 B:190 C:179 D:67] map[B:90 C:52 D:98 A:64] map[A:100 B:60 C:170 D:120]]
Upvotes: 7