main
Raw Download raw file
 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
 5package utils
 6
 7import "sort"
 8
 9// Sort sorts values (in-place) with respect to the given comparator.
10//
11// Uses Go's sort (hybrid of quicksort for large and then insertion sort for smaller slices).
12func Sort(values []interface{}, comparator Comparator) {
13	sort.Sort(sortable{values, comparator})
14}
15
16type sortable struct {
17	values     []interface{}
18	comparator Comparator
19}
20
21func (s sortable) Len() int {
22	return len(s.values)
23}
24func (s sortable) Swap(i, j int) {
25	s.values[i], s.values[j] = s.values[j], s.values[i]
26}
27func (s sortable) Less(i, j int) bool {
28	return s.comparator(s.values[i], s.values[j]) < 0
29}