One, shrink sort
Package Mainimport ("FMT" "Sort" "Strings")varoriginal = []string{ "nonmentals", "hydrogen", "Carbon", "nitrogenl", "Oxygen", "Inner transitionals", "lanthanides", "europium", "cerium", "actinides", "Uranium", "Plutonium", "Curium", "Alkali Metals", "Lithium", "Sodium", "Potassium",}func Main () {FMT. Println ("| Original | Sorted |") fmt. Println ("|----------------------|-------------------|") Sorted:=sortedindentedstrings (original) forI: =Range Original {fmt. Printf ("|%-19s|%-19s|\n", Original[i], sorted[i])}} Func sortedindentedstrings (Slice []string) []string{entries:=populatedentries (Slice)returnsortedentries (entries)}func populatedentries (Slice []string) Entries {indent, indentsize:=computeindent (Slice) fmt. Printf ("[%s]%d =%d\n", indent, Len (indent), indentsize) entries:= Make (Entries,0) for_, Item: =Range Slice {i, Level:=0,0 forstrings. Hasprefix (item[i:], indent) {i+=indentsize Level++} key:=strings. ToLower (Strings. Trimspace (item)) AddEntry (level, key, item,&entries)} returnentries}func computeindent (Slice []string) (string,int) { for_, Item: =Range Slice {ifLen (item) >0&& (item[0] ==' '|| item[0] =='\ t') {whitspace:= Rune (item[0]) forIChar: = Range item[1:] { if Char!=Whitspace {i++returnStrings. Repeat (string(Whitspace), i), i} }}}return "",0}func addentry ( levelint, key, valuestring, Entries *Entries) { ifLevel = =0 { *entries = Append (*entries, Entry{key, value, make (entries,0)}) } Else{addentry ( level-1, key, value,& ((*entries) [entries. Len ()-1].children))}}func sortedentries (entries entries) []string { varIndentedslice []stringsort. Sort (entries) for_, Entry: =Range Entries {populatedindentedstrings (entry,&Indentedslice)} returnIndentedslice}func populatedindentedstrings (Entry entry, Indentedslice*[]string) { *indentedslice = Append (*Indentedslice, Entry.value) sort. Sort (Entry.children) for_, Child: =range Entry.children {populatedindentedstrings (Child, Indentedslice)}}type entrystruct{Keystringvaluestringchildren Entries}type Entries []entryfunc (Entries Entries) Len ()int{returnlen (entries)}func (entries entries) less (i, Jint)BOOL { returnEntries[i].key <Entries[j].key}func (entries entries) Swap (i, Jint) {entries[i], Entries[j]=Entries[j], Entries[i]}
This code is still very simple, but not perfect, because there is a hypothetical premise-all lines of text use the same tightening.
Code logic:
The first step is to iterate through all the rows of data until a row is found, the text that begins with a space tab, and how many of the characters in the line are used as the basis for judging the tightening level.
It then facilitates all row data again, computes the string "start" repeatedly with String.hasprefix, and calculates the result as a shrink level.
To construct a data structure using the Shrink level entry, tighten to 0, then add the root entrie, tighten to 1, then on the child of the root entry, build entry, 2, on the child of entry to build on the child.
Finally, a comparison of strings using the less defined method is used to sort each layer of each root entry.
Using the data structure, if you assume that there is a entry node called root, you can think of a tree.
"Go Language Programming" study (VI)