mirror of
https://github.com/jbranchaud/til
synced 2026-01-20 23:48:02 +00:00
Compare commits
4 Commits
36b90c5232
...
6705078280
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6705078280 | ||
|
|
7274b3d563 | ||
|
|
5615da920f | ||
|
|
c60c63f554 |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1533 TILs and counting..._
|
_1534 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -191,7 +191,7 @@ _1533 TILs and counting..._
|
|||||||
- [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md)
|
- [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md)
|
||||||
- [Allow Cross-Origin Requests To Include Cookies](devops/allow-cross-origin-requests-to-include-cookies.md)
|
- [Allow Cross-Origin Requests To Include Cookies](devops/allow-cross-origin-requests-to-include-cookies.md)
|
||||||
- [Allow HTTPS Through Your UFW Firewall](devops/allow-https-through-your-ufw-firewall.md)
|
- [Allow HTTPS Through Your UFW Firewall](devops/allow-https-through-your-ufw-firewall.md)
|
||||||
- [Check For Cached Site Assocation File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
|
- [Check For Cached Site Association File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
|
||||||
- [Check The Status of All Services](devops/check-the-status-of-all-services.md)
|
- [Check The Status of All Services](devops/check-the-status-of-all-services.md)
|
||||||
- [Check The Syntax Of nginx Files](devops/check-the-syntax-of-nginx-files.md)
|
- [Check The Syntax Of nginx Files](devops/check-the-syntax-of-nginx-files.md)
|
||||||
- [Connect To An RDS PostgreSQL Database](devops/connect-to-an-rds-postgresql-database.md)
|
- [Connect To An RDS PostgreSQL Database](devops/connect-to-an-rds-postgresql-database.md)
|
||||||
@@ -408,6 +408,7 @@ _1533 TILs and counting..._
|
|||||||
- [Parse Flags From CLI Arguments](go/parse-flags-from-cli-arguments.md)
|
- [Parse Flags From CLI Arguments](go/parse-flags-from-cli-arguments.md)
|
||||||
- [Replace The Current Process With An External Command](go/replace-the-current-process-with-an-external-command.md)
|
- [Replace The Current Process With An External Command](go/replace-the-current-process-with-an-external-command.md)
|
||||||
- [Sleep For A Duration](go/sleep-for-a-duration.md)
|
- [Sleep For A Duration](go/sleep-for-a-duration.md)
|
||||||
|
- [Sort Slice In Ascending Or Descending Order](go/sort-slice-in-ascending-or-descending-order.md)
|
||||||
- [Upgrading From An Older Version On Mac](go/upgrading-from-an-older-version-on-mac.md)
|
- [Upgrading From An Older Version On Mac](go/upgrading-from-an-older-version-on-mac.md)
|
||||||
|
|
||||||
### GROQ
|
### GROQ
|
||||||
@@ -737,7 +738,7 @@ _1533 TILs and counting..._
|
|||||||
- [Check If Clusters Are Upgrade Compatible](postgres/check-if-clusters-are-upgrade-compatible.md)
|
- [Check If Clusters Are Upgrade Compatible](postgres/check-if-clusters-are-upgrade-compatible.md)
|
||||||
- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md)
|
- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md)
|
||||||
- [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md)
|
- [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md)
|
||||||
- [Check Table For Any Oprhaned Records](postgres/check-table-for-any-orphaned-records.md)
|
- [Check Table For Any Orphaned Records](postgres/check-table-for-any-orphaned-records.md)
|
||||||
- [Checking Inequality](postgres/checking-inequality.md)
|
- [Checking Inequality](postgres/checking-inequality.md)
|
||||||
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
|
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
|
||||||
- [Clear The Screen In psql](postgres/clear-the-screen-in-psql.md)
|
- [Clear The Screen In psql](postgres/clear-the-screen-in-psql.md)
|
||||||
|
|||||||
52
go/sort-slice-in-ascending-or-descending-order.md
Normal file
52
go/sort-slice-in-ascending-or-descending-order.md
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
# Sort Slice In Ascending Or Descending Order
|
||||||
|
|
||||||
|
The [`slices.Sort`](https://pkg.go.dev/slices#Sort) function defaults to
|
||||||
|
sorting a slice in ascending order. If we want to control the sort order, we
|
||||||
|
have to do a little more work. We can reach for the
|
||||||
|
[`slices.SortFunc`](https://pkg.go.dev/slices#SortFunc) function. This allows
|
||||||
|
us to define a sort function and in that function we can control whether the
|
||||||
|
sort order is ascending or descending.
|
||||||
|
|
||||||
|
Here I've defined `SortItems` which takes a list of items constrained by the
|
||||||
|
[`cmp.Ordered`](https://pkg.go.dev/cmp#Ordered) interface (so things like
|
||||||
|
`int`, `string`, `uint64`, etc.). It takes a direction (`ASC` or `DESC`) as a
|
||||||
|
second argument. It does the directional sort based on that second argument.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"cmp"
|
||||||
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Direction int
|
||||||
|
|
||||||
|
const (
|
||||||
|
ASC Direction = iota
|
||||||
|
DESC
|
||||||
|
)
|
||||||
|
|
||||||
|
func SortItems[T cmp.Ordered](items []T, dir Direction) {
|
||||||
|
slices.SortFunc(items, func(i, j T) int {
|
||||||
|
if dir == ASC {
|
||||||
|
return cmp.Compare(i, j)
|
||||||
|
} else if dir == DESC {
|
||||||
|
return cmp.Compare(j, i)
|
||||||
|
} else {
|
||||||
|
panic(fmt.Sprintf("Unrecognized sort direction: %d", dir))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// items := []int{3,2,8,1}
|
||||||
|
// SortItems(items, ASC)
|
||||||
|
// // items => [1,2,3,8]
|
||||||
|
// SortItems(items, DESC)
|
||||||
|
// // items => [8,3,2,1]
|
||||||
|
```
|
||||||
|
|
||||||
|
Because `slices.SortFunc` expects a negative value, zero, or positive value to
|
||||||
|
determine the sort order, we use
|
||||||
|
[`cmp.Compare`](https://pkg.go.dev/cmp#Compare) which returns those kinds of
|
||||||
|
values. For ascending, we compare `i` to `j`. For descending, we swap them,
|
||||||
|
comparing `j` to `i` to get the reverse sort order.
|
||||||
Reference in New Issue
Block a user