<commit_before>// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package table

import (
	"sort"

	"github.com/aclements/go-gg/generic"
)

// Sort sorts each group of g by the named column. If the column's
// type implements sort.Interface, rows will be sorted according to
// that order. Otherwise, the values in the column must be naturally
// ordered (their types must be orderable by the Go specification). If
// neither is true, Sort panics with a *generic.TypeError.
func Sort(g Grouping, col string) Grouping {
	// TODO: Consider a generic MapConcatGroups.

	// Sort each group.
	out := Grouping(new(Table))
	for _, gid := range g.Groups() {
		t := g.Table(gid)
		seq := t.MustColumn(col)
		sorter := generic.Sorter(seq)

		if sort.IsSorted(sorter) {
			// Avoid shuffling everything by the identity
			// permutation.
			out.AddTable(gid, t)
			continue
		}

		// Generate an initial permutation sequence.<commit_msg>table: Sort -> SortBy for consistency with GroupBy<commit_after>// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package table

import (
	"sort"

	"github.com/aclements/go-gg/generic"
)

// SortBy sorts each group of g by the named column. If the column's
// type implements sort.Interface, rows will be sorted according to
// that order. Otherwise, the values in the column must be naturally
// ordered (their types must be orderable by the Go specification). If
// neither is true, SortBy panics with a *generic.TypeError.
func SortBy(g Grouping, col string) Grouping {
	// TODO: Consider a generic MapConcatGroups.

	// Sort each group.
	out := Grouping(new(Table))
	for _, gid := range g.Groups() {
		t := g.Table(gid)
		seq := t.MustColumn(col)
		sorter := generic.Sorter(seq)

		if sort.IsSorted(sorter) {
			// Avoid shuffling everything by the identity
			// permutation.
			out.AddTable(gid, t)
			continue
		}

		// Generate an initial permutation sequence.
{'fraction_non_alphanumeric': 0.09891150023663038, 'fraction_numerical': 0.003786086133459536, 'mean_word_length': 3.3408624229979464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7339889', 'n_tokens_mistral': 681, 'n_tokens_neox': 614, 'n_words': 261}