Debian Codegraph @

examples/customcomparator/customcomparator.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package main
6
7 import (
8 "fmt"
9
10 "github.com/emirpasic/gods/v2/sets/treeset"
11 )
12
13 // User model (id and name)
14 type User struct {
15 id int
16 name string
17 }
18
19 // Comparator function (sort by IDs)
20 func byID(a, b User) int {
21
22 switch {
23 case a.id > b.id:
24 return 1
25 case a.id < b.id:
26 return -1
27 default:
28 return 0
29 }
30 }
31
32 // CustomComparatorExample to demonstrate basic usage of CustomComparator
33 func main() {
34 set := treeset.NewWith(byID)
35
36 set.Add(User{2, "Second"})
37 set.Add(User{3, "Third"})
38 set.Add(User{1, "First"})
39 set.Add(User{4, "Fourth"})
40
41 fmt.Println(set) // {1 First}, {2 Second}, {3 Third}, {4 Fourth}
42 }