Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Playground

coinsort.gno

// Package coinsort provides helpers to sort a slice of banker.Coins using the
// classic sort.Sort API (without relying on sort.Slice).
//
// Usage examples:
//
//	coins := banker.GetCoins("g1....")
//
//	// Ascending by balance
//	coinsort.SortByBalance(coins)
//
//	// Custom order – largest balance first
//	coinsort.SortBy(coins, func(a, b chain.Coin) bool {
//	    return a.Amount > b.Amount // descending
//	})
//
// Note: when getting banker.Coins from the banker, it's sorted by denom by default.
package coinsort

import (
	"chain"
	"sort"
)

type ByAmount struct{ chain.Coins }

func (b ByAmount) Len() int           { return len(b.Coins) }
func (b ByAmount) Swap(i, j int)      { b.Coins[i], b.Coins[j] = b.Coins[j], b.Coins[i] }
func (b ByAmount) Less(i, j int) bool { return b.Coins[i].Amount < b.Coins[j].Amount }

// SortByBalance sorts c in ascending order by Amount.
//
//	coinsort.SortByBalance(myCoins)
func SortByBalance(c chain.Coins) {
	sort.Sort(ByAmount{c})
}

// LessFunc defines the comparison function for SortBy. It must return true if
// 'a' should come before 'b'.

type LessFunc func(a, b chain.Coin) bool

// customSorter adapts a LessFunc to sort.Interface so we can keep using
// sort.Sort (rather than sort.Slice).

type customSorter struct {
	coins chain.Coins
	less  LessFunc
}

func (cs customSorter) Len() int      { return len(cs.coins) }
func (cs customSorter) Swap(i, j int) { cs.coins[i], cs.coins[j] = cs.coins[j], cs.coins[i] }
func (cs customSorter) Less(i, j int) bool {
	return cs.less(cs.coins[i], cs.coins[j])
}

// SortBy sorts c in place using the provided LessFunc.
//
// Example – descending by Amount:
//
//	coinsort.SortBy(coins, func(a, b banker.Coin) bool {
//	    return a.Amount > b.Amount
//	})
func SortBy(c chain.Coins, less LessFunc) {
	if less == nil {
		return // nothing to do; keep original order
	}
	sort.Sort(customSorter{coins: c, less: less})
}

coinsort_test.gno

package coinsort

import (
	"chain"
	"testing"
)

func TestSortByBalance(t *testing.T) {
	coins := chain.Coins{
		chain.Coin{Denom: "b", Amount: 50},
		chain.Coin{Denom: "c", Amount: 10},
		chain.Coin{Denom: "a", Amount: 100},
	}

	expected := chain.Coins{
		chain.Coin{Denom: "c", Amount: 10},
		chain.Coin{Denom: "b", Amount: 50},
		chain.Coin{Denom: "a", Amount: 100},
	}

	SortByBalance(coins)

	for i := range coins {
		if coins[i] != expected[i] {
			t.Errorf("SortByBalance failed at index %d: got %+v, want %+v", i, coins[i], expected[i])
		}
	}
}

func TestSortByCustomDescendingAmount(t *testing.T) {
	coins := chain.Coins{
		chain.Coin{Denom: "a", Amount: 2},
		chain.Coin{Denom: "b", Amount: 3},
		chain.Coin{Denom: "c", Amount: 1},
	}

	expected := chain.Coins{
		chain.Coin{Denom: "b", Amount: 3},
		chain.Coin{Denom: "a", Amount: 2},
		chain.Coin{Denom: "c", Amount: 1},
	}

	SortBy(coins, func(a, b chain.Coin) bool {
		return a.Amount > b.Amount // descending
	})

	for i := range coins {
		if coins[i] != expected[i] {
			t.Errorf("SortBy custom descending failed at index %d: got %+v, want %+v", i, coins[i], expected[i])
		}
	}
}

func TestSortByNilFunc(t *testing.T) {
	coins := chain.Coins{
		chain.Coin{Denom: "x", Amount: 5},
		chain.Coin{Denom: "z", Amount: 20},
		chain.Coin{Denom: "y", Amount: 10},
	}

	expected := chain.Coins{
		chain.Coin{Denom: "x", Amount: 5},
		chain.Coin{Denom: "z", Amount: 20},
		chain.Coin{Denom: "y", Amount: 10},
	}

	SortBy(coins, nil)

	// should stay the same
	for i := range coins {
		if coins[i] != expected[i] {
			t.Errorf("SortBy nil func failed at index %d: got %+v, want %+v", i, coins[i], expected[i])
		}
	}
}

gnomod.toml

module = "gno.land/p/leon/coinsort/v0"
gno = "0.9"

[addpkg]
  creator = "g125em6arxsnj49vx35f0n0z34putv5ty3376fg5"