Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Some of the motivation behind this library is discussed in this [blog post](http
* [`DropRight`](#_droprightslice-n)
* [`Chain`](#_chainsliceactionactionvalue)
* [`Value`](#_chainsliceactionactionvalue)
* [`Sort`](#_sortslice)

 
#### `_.Reverse(slice)`
Expand Down Expand Up @@ -130,6 +131,18 @@ _int.Chain([]int{1, 2, 1, 3}).Uniq().Reverse().Value()
// => []int{3, 2, 1}
```

#### `_.Sort(slice, func)`

Returns a new array of all elements in their sorted order according to the provided comparison function.

```go
compare := func (a, b int) bool {
return a < b
}
_int.Sort([]int{2, 5, 8, 0, 1}, compare)
// => []int{0, 1, 2, 5, 8}
```

&nbsp;
## Working with different types

Expand Down
40 changes: 40 additions & 0 deletions templates/_ComplexType/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package __ComplexType

import (
. "_ImportLocation"
"sort"
)

// Type ...
type Type struct {
typ []_ComplexType
cmp func(a, b _ComplexType) bool
}

func (obj Type) Len() int {
return len(obj.typ)
}

func (obj Type) Swap(i, j int) {
obj.typ[i], obj.typ[j] = obj.typ[j], obj.typ[i]
}

func (obj Type) Less(i, j int) bool {
return obj.cmp(obj.typ[i], obj.typ[j])
}

// Sort ...
func Sort(slice []_ComplexType, cmp func(a, b _ComplexType) bool) (res []_ComplexType) {
res = make([]_ComplexType, 0, len(slice))
res = append(res, slice...)
val := Type{
typ: res,
cmp: cmp,
}
sort.Sort(val)
return
}

func (c *chain) Sort(cmp func(a, b _ComplexType) bool) *chain {
return &chain{value: Sort(c.value, cmp)}
}
39 changes: 39 additions & 0 deletions templates/_SimpleType/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package __SimpleType

import (
"sort"
)

// Type ...
type Type struct {
typ []_SimpleType
cmp func(a, b _SimpleType) bool
}

func (obj Type) Len() int {
return len(obj.typ)
}

func (obj Type) Swap(i, j int) {
obj.typ[i], obj.typ[j] = obj.typ[j], obj.typ[i]
}

func (obj Type) Less(i, j int) bool {
return obj.cmp(obj.typ[i], obj.typ[j])
}

// Sort ...
func Sort(slice []_SimpleType, cmp func(a, b _SimpleType) bool) (res []_SimpleType) {
res = make([]_SimpleType, 0, len(slice))
res = append(res, slice...)
val := Type{
typ: res,
cmp: cmp,
}
sort.Sort(val)
return
}

func (c *chain) Sort(cmp func(a, b _SimpleType) bool) *chain {
return &chain{value: Sort(c.value, cmp)}
}
102 changes: 102 additions & 0 deletions tests/sort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package tests

import (
"reflect"
"testing"

"github.com/go-dash/slice/_Person" // github.com/go-dash/slice/tests/types
"github.com/go-dash/slice/_int"
"github.com/go-dash/slice/_string"
. "github.com/go-dash/slice/tests/types"
)

var tableSortString = []struct {
input []string
output []string
}{
{nil, []string{}},
{[]string{}, []string{}},
{[]string{"one", "two", "three", "four"}, []string{"four", "one", "three", "two"}},
}

func TestSortString(t *testing.T) {
for _, tt := range tableSortString {
res := _string.Sort(tt.input, func(a, b string) bool {
return a <= b
})
if !reflect.DeepEqual(res, tt.output) {
t.Fatalf("Expected %v received %v", tt.output, res)
}
res = _string.Chain(tt.input).Sort(func(a, b string) bool {
return a <= b
}).Value()
if !reflect.DeepEqual(res, tt.output) {
t.Fatalf("Expected %v received %v", tt.output, res)
}
}
}

var tableSortInt = []struct {
input []int
output []int
}{
{nil, []int{}},
{[]int{}, []int{}},
{[]int{10, 2, 5, 8, 5, 6}, []int{2, 5, 5, 6, 8, 10}},
}

func TestSortInt(t *testing.T) {
for _, tt := range tableSortInt {
res := _int.Sort(tt.input, func(a, b int) bool {
return a <= b
})
if !reflect.DeepEqual(res, tt.output) {
t.Fatalf("Expected %v received %v", tt.output, res)
}
res = _int.Chain(tt.input).Sort(func(a, b int) bool {
return a <= b
}).Value()
if !reflect.DeepEqual(res, tt.output) {
t.Fatalf("Expected %v received %v", tt.output, res)
}
}
}

var tableSortPerson = []struct {
input []Person
output []Person
}{
{nil, []Person{}},
{[]Person{}, []Person{}},
{
[]Person{Person{"das", 1018}, Person{"sdbb", 109}, Person{"fecc", 122}, Person{"adad", 122}, Person{"rfaa", 122}, Person{"bb", 1209}, Person{"cc", 250}, Person{"dd", 210}},
[]Person{Person{"sdbb", 109}, Person{"adad", 122}, Person{"fecc", 122}, Person{"rfaa", 122}, Person{"dd", 210}, Person{"cc", 250}, Person{"das", 1018}, Person{"bb", 1209}},
},
}

func TestSortPerson(t *testing.T) {
for _, tt := range tableSortPerson {
res := _Person.Sort(tt.input, func(a, b Person) bool {
if a.Age < b.Age {
return true
} else if a.Age > b.Age {
return false
}
return a.Name < b.Name
})
if !reflect.DeepEqual(res, tt.output) {
t.Fatalf("Expected %v received %v", tt.output, res)
}
res = _Person.Chain(tt.input).Sort(func(a, b Person) bool {
if a.Age < b.Age {
return true
} else if a.Age > b.Age {
return false
}
return a.Name < b.Name
}).Value()
if !reflect.DeepEqual(res, tt.output) {
t.Fatalf("Expected %v received %v", tt.output, res)
}
}
}