// Returns sorted container's elements using with respect to the passed comparator. // Does not effect the ordering of elements within the container. // Uses timsort. func GetSortedValues(container Interface, comparator utils.Comparator) []interface{} { values := container.Values() if len(values) < 2 { return values } utils.Sort(values, comparator) return values }
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"] }
// Sort sort values (in-place) using. func (list *List) Sort(comparator utils.Comparator) { if list.size < 2 { return } values := list.Values() utils.Sort(values, comparator) list.Clear() list.Add(values...) }
// Sorts values (in-place) using timsort. func (list *List) Sort(comparator utils.Comparator) { if len(list.elements) < 2 { return } utils.Sort(list.elements[:list.size], comparator) }
func (a *Array) Sort(comparator utils.Comparator) { if len(a.elements) < 2 { return } utils.Sort(a.elements[:a.size], comparator) }