|
1
|
|
|
package gotil |
|
2
|
|
|
|
|
3
|
|
|
import "github.com/gotilty/gotil/internal" |
|
4
|
|
|
|
|
5
|
|
|
// Sort returns a new array of ordered values as ascending, using a version of Merge Algorithm. |
|
6
|
|
|
// data := []int64{100, 30, -100, -5} |
|
7
|
|
|
// newData, _ := Sort(data) |
|
8
|
|
|
// // Output: [-100 -5 30 100] |
|
9
|
|
|
func Sort(a interface{}) (interface{}, error) { |
|
10
|
|
|
return internal.Sort(a) |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
// SortDesc returns a new array of ordered values as descending, using a version of Merge Algorithm. |
|
14
|
|
|
// |
|
15
|
|
|
// data := []int64{100, 30, -100, -5} |
|
16
|
|
|
// newData, _ := SortDesc(data) |
|
17
|
|
|
// // Output: [100 30 -5 -100] |
|
18
|
|
|
func SortDesc(a interface{}) (interface{}, error) { |
|
19
|
|
|
return internal.SortDesc(a) |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
// SortBy returns a new array of ordered values as ascending, using a version of Merge Algorithm with a given property path list of struct |
|
23
|
|
|
// Example: SortBy(data, "location.city") |
|
24
|
|
|
// data := []user{ |
|
25
|
|
|
// { |
|
26
|
|
|
// name: "Micheal", |
|
27
|
|
|
// age: 27, |
|
28
|
|
|
// location: location{ |
|
29
|
|
|
// city: "New York", |
|
30
|
|
|
// }, |
|
31
|
|
|
// }, |
|
32
|
|
|
// { |
|
33
|
|
|
// name: "Joe", |
|
34
|
|
|
// age: 30, |
|
35
|
|
|
// location: location{ |
|
36
|
|
|
// city: "Detroit", |
|
37
|
|
|
// }, |
|
38
|
|
|
// }, |
|
39
|
|
|
// } |
|
40
|
|
|
// newData, _ := SortBy(data, "location.city") |
|
41
|
|
|
// // Output: [{Joe 30 {Detroit}} {Micheal 27 {New York}}] |
|
42
|
|
|
func SortBy(a interface{}, path string) (interface{}, error) { |
|
43
|
|
|
return internal.SortBy(a, path) |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// SortDescBy returns a new array of ordered values as descending, using a version of Merge Algorithm with a given property path list of struct |
|
47
|
|
|
// Example: SortDescBy(data, "location.city") |
|
48
|
|
|
// data := []user{ |
|
49
|
|
|
// { |
|
50
|
|
|
// name: "Micheal", |
|
51
|
|
|
// age: 27, |
|
52
|
|
|
// location: location{ |
|
53
|
|
|
// city: "New York", |
|
54
|
|
|
// }, |
|
55
|
|
|
// }, |
|
56
|
|
|
// { |
|
57
|
|
|
// name: "Joe", |
|
58
|
|
|
// age: 30, |
|
59
|
|
|
// location: location{ |
|
60
|
|
|
// city: "Detroit", |
|
61
|
|
|
// }, |
|
62
|
|
|
// }, |
|
63
|
|
|
// } |
|
64
|
|
|
// newData, _ := SortDescBy(data, "location.city") |
|
65
|
|
|
// // Output: [{Micheal 27 {New York}} {Joe 30 {Detroit}}] |
|
66
|
|
|
func SortDescBy(a interface{}, path string) (interface{}, error) { |
|
67
|
|
|
return internal.SortDescBy(a, path) |
|
68
|
|
|
} |
|
69
|
|
|
|