Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom comparator example #271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 8 additions & 11 deletions examples/customcomparator/customcomparator.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"fmt"

"github.com/emirpasic/gods/v2/sets/treeset"
"github.com/emirpasic/gods/sets/treeset"
)

// User model (id and name)
type User struct {
id int
name string
}

// Comparator function (sort by IDs)
func byID(a, b User) int {
// Custom comparator (sort by IDs)
func byID(a, b interface{}) int {
// Type assertion, program will panic if this is not respected
c1 := a.(User)
c2 := b.(User)

switch {
case a.id > b.id:
case c1.id > c2.id:
return 1
case a.id < b.id:
case c1.id < c2.id:
return -1
default:
return 0
}
}

// CustomComparatorExample to demonstrate basic usage of CustomComparator
func main() {
set := treeset.NewWith(byID)

Expand Down