From 7c7da288e2300217f3097f2ff6edf8a6a3e76a52 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Sun, 8 Mar 2015 03:27:27 +0100 Subject: [PATCH] examples update on sorts --- README.md | 4 +--- examples/arraylist.go | 8 ++++++-- examples/sort.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 examples/sort.go diff --git a/README.md b/README.md index 3a8b699..67ab234 100644 --- a/README.md +++ b/README.md @@ -529,9 +529,7 @@ Internally they use the _utils.Sort()_ method: ```go package main -import ( - "github.com/emirpasic/gods/utils" -) +import "github.com/emirpasic/gods/utils" func main() { strings := []interface{}{} // [] diff --git a/examples/arraylist.go b/examples/arraylist.go index 418a5ef..877e623 100644 --- a/examples/arraylist.go +++ b/examples/arraylist.go @@ -26,12 +26,16 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package examples -import "github.com/emirpasic/gods/lists/arraylist" +import ( + "github.com/emirpasic/gods/lists/arraylist" + "github.com/emirpasic/gods/utils" +) func ArrayListExample() { list := arraylist.New() list.Add("a") // ["a"] - list.Add("b", "c") // ["a","b","c"] + list.Add("c", "b") // ["a","c","b"] + list.Sort(utils.StringComparator) // ["a","b","c"] _, _ = list.Get(0) // "a",true _, _ = list.Get(100) // nil,false _ = list.Contains("a", "b", "c") // true diff --git a/examples/sort.go b/examples/sort.go new file mode 100644 index 0000000..b713379 --- /dev/null +++ b/examples/sort.go @@ -0,0 +1,38 @@ +/* +Copyright (c) 2015, Emir Pasic +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +package examples + +import "github.com/emirpasic/gods/utils" + +func SortExample() { + strings := []interface{}{} // [] + strings = append(strings, "d") // ["d"] + strings = append(strings, "a") // ["d","a"] + strings = append(strings, "b") // ["d","a",b" + strings = append(strings, "c") // ["d","a",b","c"] + utils.Sort(strings, utils.StringComparator) // ["a","b","c","d"] +}