// Topologically sorts an array of packages
338296
func Sort(releasePackages []*Package) ([]*Package, error) {
	sortedPackages := []*Package{}

	incomingEdges, outgoingEdges := getEdgeMaps(releasePackages)
	noIncomingEdgesSet := []*Package{}

	for pkg, edgeList := range incomingEdges {
		if len(edgeList) == 0 {
			noIncomingEdgesSet = append(noIncomingEdgesSet, pkg)
		}
	}
	for len(noIncomingEdgesSet) > 0 {
		elem := noIncomingEdgesSet[0]
		noIncomingEdgesSet = noIncomingEdgesSet[1:]

		sortedPackages = append([]*Package{elem}, sortedPackages...)

		for _, pkg := range outgoingEdges[elem] {
			incomingEdges[pkg] = removeFromList(incomingEdges[pkg], elem)
			if len(incomingEdges[pkg]) == 0 {
				noIncomingEdgesSet = append(noIncomingEdgesSet, pkg)
			}
		}
	}
	for _, edges := range incomingEdges {
		if len(edges) > 0 {
			return nil, errors.New("Circular dependency detected while sorting packages")
		}
	}
	return sortedPackages, nil
}
580328
func MatchTopologySelectorTerms(topologySelectorTerms []v1.TopologySelectorTerm, lbls labels.Set) bool {
	if len(topologySelectorTerms) == 0 {
		// empty term list matches all objects
		return true
	}

	for _, req := range topologySelectorTerms {
		// nil or empty term selects no objects
		if len(req.MatchLabelExpressions) == 0 {
			continue
		}

		labelSelector, err := TopologySelectorRequirementsAsSelector(req.MatchLabelExpressions)
		if err != nil || !labelSelector.Matches(lbls) {
			continue
		}

		return true
	}

	return false
}
364068
func Sort(list cty.Value) (cty.Value, error) {
	return SortFunc.Call([]cty.Value{list})
}
752802
func SortStringPairs(pairs []*pb.StringPair) {
	sort.Slice(pairs, func(i, j int) bool {
		switch {
		case pairs[i].Key < pairs[j].Key:
			return true
		case pairs[i].Key > pairs[j].Key:
			return false
		default:
			return pairs[i].Value < pairs[j].Value
		}
	})
}
479504
func (api *DiskAPI) SortByConnectionOrder(reverse bool) *DiskAPI {
	api.sortBy("ConnectionOrder", reverse)
	return api
}
356764
func (api *IconAPI) SortBy(key string, reverse bool) *IconAPI {
	api.sortBy(key, reverse)
	return api
}
192920
func (array orderTabletsForSwap) Less(i, j int) bool {
	return tabletSortIndex(&array[i]) < tabletSortIndex(&array[j])
}
138725
func (api *ServerAPI) SortByCPU(reverse bool) *ServerAPI {
	api.sortBy("ServerPlan.CPU", reverse)
	return api
}
1000969
func (c *CloudAPI) ListPackages(filters map[string]string) ([]cloudapi.Package, error) {
	if err := c.ProcessFunctionHook(c, filters); err != nil {
		return nil, err
	}

	availablePackages := c.packages

	if filters != nil {
		for k, f := range filters {
			// check if valid filter
			if contains(packagesFilters, k) {
				pkgs := []cloudapi.Package{}
				// filter from availablePackages and add to pkgs
				for _, p := range availablePackages {
					if k == "name" && p.Name == f {
						pkgs = append(pkgs, p)
					} else if k == "memory" {
						i, err := strconv.Atoi(f)
						if err == nil && p.Memory == i {
							pkgs = append(pkgs, p)
						}
					} else if k == "disk" {
						i, err := strconv.Atoi(f)
						if err == nil && p.Disk == i {
							pkgs = append(pkgs, p)
						}
					} else if k == "swap" {
						i, err := strconv.Atoi(f)
						if err == nil && p.Swap == i {
							pkgs = append(pkgs, p)
						}
					} else if k == "version" && p.Version == f {
						pkgs = append(pkgs, p)
					} else if k == "vcpus" {
						i, err := strconv.Atoi(f)
						if err == nil && p.VCPUs == i {
							pkgs = append(pkgs, p)
						}
					} else if k == "group" && p.Group == f {
						pkgs = append(pkgs, p)
					}
				}
				availablePackages = pkgs
			}
		}
	}

	return availablePackages, nil
}
595882
func (api *CDROMAPI) SortBySize(reverse bool) *CDROMAPI {
	api.sortBy("SizeMB", reverse)
	return api
}
642046
func SortExtensions(m map[string]Extension) ([]Extension, error) {
	pMap := make(map[int]Extension)
	priorities := make([]int, 0, len(m))
	for n, ext := range m {
		p := ext.Priority
		if _, exist := pMap[p]; exist {
			err := fmt.Errorf("duplicate priority %d on %s", p, n)
			return nil, err
		}
		pMap[p] = ext
		priorities = append(priorities, p)
	}

	sort.Ints(priorities)

	result := make([]Extension, len(priorities))
	for i, p := range priorities {
		result[i] = pMap[p]
	}

	return result, nil
}
507684
func (g *Graph) orderedRelatives(key *Key, attr, orderBy string, cb func(n *Node) []*Node) ([]*Node, error) {
	if !g.finalized {
		return nil, ErrNotFinalized
	}
	if err := g.validateKey(attr, key); err != nil {
		return nil, err
	}
	if err := validateOrder(orderBy); err != nil {
		return nil, err
	}
	if n := g.nodes[key]; n != nil {
		return sortByEdgeOrder(cb(n), orderBy), nil
	}
	return nil, nil // no node at all -> no related nodes
}
1141062
func SortRoots() ([]Root, error) {
	if len(roots) == 0 {
		return nil, nil
	}
	// First flatten dependencies for each root
	rootDeps := make(map[string][]Root, len(roots))
	rootByName := make(map[string]Root, len(roots))
	for _, r := range roots {
		sorted := sortDependencies(r, func(r Root) []Root { return r.DependsOn() })
		length := len(sorted)
		for i := 0; i < length/2; i++ {
			sorted[i], sorted[length-i-1] = sorted[length-i-1], sorted[i]
		}
		rootDeps[r.DSLName()] = sorted
		rootByName[r.DSLName()] = r
	}
	// Now check for cycles
	for name, deps := range rootDeps {
		root := rootByName[name]
		for otherName, otherdeps := range rootDeps {
			other := rootByName[otherName]
			if root.DSLName() == other.DSLName() {
				continue
			}
			dependsOnOther := false
			for _, dep := range deps {
				if dep.DSLName() == other.DSLName() {
					dependsOnOther = true
					break
				}
			}
			if dependsOnOther {
				for _, dep := range otherdeps {
					if dep.DSLName() == root.DSLName() {
						return nil, fmt.Errorf("dependency cycle: %s and %s depend on each other (directly or not)",
							root.DSLName(), other.DSLName())
					}
				}
			}
		}
	}
	// Now sort top level DSLs
	var sorted []Root
	for _, r := range roots {
		s := sortDependencies(r, func(r Root) []Root { return rootDeps[r.DSLName()] })
		for _, s := range s {
			found := false
			for _, r := range sorted {
				if r.DSLName() == s.DSLName() {
					found = true
					break
				}
			}
			if !found {
				sorted = append(sorted, s)
			}
		}
	}
	return sorted, nil
}
1092953
func Sort(list []string) []string {
	sorted := sort.StringSlice(list)
	sorted.Sort()
	return sorted
}
1290300
func reverseSortMapByValue(m map[string]int) pairList {
	p := make(pairList, len(m))
	i := 0
	for k, v := range m {
		p[i] = pair{k, v}
		i = i + 1
	}
	sort.Sort(sort.Reverse(p))
	return p
}
1027567
func (t *Manager) GetSortedAPIsByPkgPath() (results []Definition) {
	sorter := newSorter(t)
	sort.Sort(sorter)
	return sorter.apis
}
131223
func (p *SortKeys) AsStringSliceComparator() StringSliceComparator {
	numeric := utils.NewIndex(p.Numeric)
	reverseIndex := utils.NewIndex(p.Reversed)
	comparators := make([]StringComparator, len(p.Keys))
	for i, k := range p.Keys {
		if numeric.Contains(k) {
			comparators[i] = LessNumericStrings
		} else {
			comparators[i] = LessStrings
		}
		if reverseIndex.Contains(k) {
			f := comparators[i]
			comparators[i] = func(l, r string) bool {
				return !f(l, r)
			}
		}
	}
	return AsStringSliceComparator(comparators)
}