{"id":18578973,"url":"https://github.com/patrickmn/sortutil","last_synced_at":"2025-04-10T10:31:22.821Z","repository":{"id":2562028,"uuid":"3541264","full_name":"patrickmn/sortutil","owner":"patrickmn","description":"Nested, case-insensitive, and reverse sorting for Go","archived":false,"fork":false,"pushed_at":"2012-05-26T08:15:26.000Z","size":184,"stargazers_count":73,"open_issues_count":2,"forks_count":16,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-04T04:32:09.430Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://patrickmn.com/projects/sortutil/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patrickmn.png","metadata":{"files":{"readme":"README","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-02-25T00:39:06.000Z","updated_at":"2025-03-01T16:56:22.000Z","dependencies_parsed_at":"2022-09-26T17:41:19.053Z","dependency_job_id":null,"html_url":"https://github.com/patrickmn/sortutil","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickmn%2Fsortutil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickmn%2Fsortutil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickmn%2Fsortutil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickmn%2Fsortutil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patrickmn","download_url":"https://codeload.github.com/patrickmn/sortutil/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248199136,"owners_count":21063641,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T23:38:27.430Z","updated_at":"2025-04-10T10:31:22.468Z","avatar_url":"https://github.com/patrickmn.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Sortutil is a Go library which lets you sort a slice without implementing a\nsort.Interface, and in different orderings: ascending, descending, or\ncase-insensitive ascending or descending (for slices of strings.)\n\nAdditionally, Sortutil lets you sort a slice of a custom struct by a given\nstruct field or index--for example, you can sort a []MyStruct by the structs'\n\"Name\" fields, or a [][]int by the second index of each nested slice, similar\nto using sorted(key=operator.itemgetter/attrgetter) in Python.\n\n== Installation\ngo get github.com/pmylund/sortutil\n\n== Documentation\ngo doc github.com/pmylund/sortutil\nor http://go.pkgdoc.org/github.com/pmylund/sortutil\n\n== Functions\nfunc Asc(slice interface{})\n    Sort a slice in ascending order.\n\nfunc AscByField(slice interface{}, name string)\n    Sort a slice in ascending order by a field name.\n\nfunc AscByFieldIndex(slice interface{}, index []int)\n    Sort a slice in ascending order by a list of nested field indices, e.g.\n    {1, 2, 3} to sort by the third field of the struct in the second field\n    of the struct in the first field of each struct in the slice.\n\nfunc AscByIndex(slice interface{}, index int)\n    Sort a slice in ascending order by an index in a child slice.\n\nfunc CiAsc(slice interface{})\n    Sort a slice in case-insensitive ascending order.\n\nfunc CiAscByField(slice interface{}, name string)\n    Sort a slice in case-insensitive ascending order by a field name. (Valid\n    for string types.)\n\nfunc CiAscByFieldIndex(slice interface{}, index []int)\n    Sort a slice in case-insensitive ascending order by a list of nested\n    field indices, e.g. {1, 2, 3} to sort by the third field of the struct\n    in the second field of the struct in the first field of each struct in\n    the slice. (Valid for string types.)\n\nfunc CiAscByIndex(slice interface{}, index int)\n    Sort a slice in case-insensitive ascending order by an index in a child\n    slice. (Valid for string types.)\n\nfunc CiDesc(slice interface{})\n    Sort a slice in case-insensitive descending order.\n\nfunc CiDescByField(slice interface{}, name string)\n    Sort a slice in case-insensitive descending order by a field name.\n    (Valid for string types.)\n\nfunc CiDescByFieldIndex(slice interface{}, index []int)\n    Sort a slice in case-insensitive descending order by a list of nested\n    field indices, e.g. {1, 2, 3} to sort by the third field of the struct\n    in the second field of the struct in the first field of each struct in\n    the slice. (Valid for string types.)\n\nfunc CiDescByIndex(slice interface{}, index int)\n    Sort a slice in case-insensitive descending order by an index in a child\n    slice. (Valid for string types.)\n\nfunc Desc(slice interface{})\n    Sort a slice in descending order.\n\nfunc DescByField(slice interface{}, name string)\n    Sort a slice in descending order by a field name.\n\nfunc DescByFieldIndex(slice interface{}, index []int)\n    Sort a slice in descending order by a list of nested field indices, e.g.\n    {1, 2, 3} to sort by the third field of the struct in the second field\n    of the struct in the first field of each struct in the slice.\n\nfunc DescByIndex(slice interface{}, index int)\n    Sort a slice in descending order by an index in a child slice.\n\nfunc Reverse(slice interface{})\n    Reverse a slice.\n\n=== Utility functions for types that already implement sort.Interface\n\nfunc ReverseInterface(s sort.Interface)\n    Reverse a type which implements sort.Interface.\n\nfunc SortReverseInterface(s sort.Interface)\n    Sort a type using its existing sort.Interface, then reverse it. For a\n    slice with a \"normal\" sort interface (where Less returns true if i is\n    less than j), this causes the slice to be sorted in descending order.\n\n== Examples\n\n=== Normal sorting\n\nints := []int{4, 7, 2, 6}\n\n// Sort the int slice in descending order, such that\n// ints[0] == 7\n// ints[1] == 6\n// ints[2] == 4\n// ints[3] == 2\nsortutil.Desc(ints)\n\nstrings := []string{\"ABC\", \"def\", \"abc\", \"GHI\"}\n\n// Sort the string slice in case-insensitive ascending order, such that:\n// strings[0] == \"ABC\"\n// strings[1] == \"abc\"\n// strings[2] == \"def\"\n// strings[3] == \"GHI\"\nsortutil.CiAsc(strings)\n\n=== Nested sorting\n\ntype MyStruct struct {\n        Id   int\n        Name string\n\tDate time.Time\n}\nnow := time.Now()\nday := 24*time.Hour\nstructs := []MyStruct{\n        {3, \"foo\", now.Add(1*day)},\n        {1, \"bar\", now.Add(-1*day)},\n        {2, \"baz\", now},\n}\n\n// Sort the slice by the Id field in ascending order, such that\n// structs[0].Id == 1\n// structs[1].Id == 2\n// structs[2].Id == 3\nsortutil.AscByField(structs, \"Id\")\n\n// Sort the slice by the Date field in ascending order, such that\n// structs[0].Date == yesterday\n// structs[1].Date == now\n// structs[2].Date == tomorrow\nsortutil.AscByField(structs, \"Date\")\n\n// Sort the slice by the Name field in descending order, such that\n// structs[0].Name == \"foo\"\n// structs[1].Name == \"baz\"\n// structs[2].Name == \"bar\"\nsortutil.DescByField(structs, \"Name\")\n\nints := [][]ints{\n        {4, 5, 1},\n        {2, 1, 7},\n        {9, 3, 3},\n        {1, 6, 2},\n}\n\n// Sort the ints by the last number in child slices in ascending\n// order, such that\n// ints[0] == {4, 5, 1}\n// ints[1] == {1, 6, 2}\n// ints[2] == {9, 3, 3}\n// ints[3] == {2, 1, 7}\nsortutil.AscByIndex(ints, 2)\n\n== Performance\nWhile sortutil is convenient, it won't beat a dedicated sort.Interface in\nterms of performance. Implementing sort.Interface for a type ByName which\nembeds e.g. []MyStruct and doing sort.Sort(ByName{MySlice}) should be\nconsidered when high performance is required.\n\nSee the top of sortutil/all_test.go, go/src/pkg/sort/example_interface_test.go,\nand go/src/pkg/sort/example_reverse_test.go for examples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickmn%2Fsortutil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatrickmn%2Fsortutil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickmn%2Fsortutil/lists"}