Grew up in Russia, lived in the States, moved to Germany, sometimes live in Spain. I program since I was 13. I used to program games, maps and now I reverse engineer password managers and other stuff
Location
Berlin and Málaga
Education
MS in CS from State Polytechnic University of St. Petersburg
And here's my take on it. You can sort by predicate. It's not exactly very efficient though, since the extension is recalculated every time. But come on, Go could be really annoying sometimes. Look at this verbosity:
Hah. You caught me! The sort should have been moved up into the print function(s) at the very least. It's a bad design decision - I put it there at first just for the sake of simplicity and never got around to cleaning it up. It doesn't really matter in a directory of a few files but would really impact performance in a larger directory. Something like the following might be fine, and still pretty simple to follow.
func plainList(m map[string][]string, v []string) {
for _, value := range v {
sort.Strings(m[value])
for _, file := range m[value] {
fmt.Println(file)
}
}
}
I think I may update the article to make sure it's called out for clarity.
funcplainlist(mmap[string][]string,orderstring)string{// 1. get all keys of the mapvarkeys[]stringfork:=rangem{keys=append(keys,k)}// 2. sort by order typeswitchorder{case"desc","Desc","DESC":forext:=rangem{sort.Sort(sort.Reverse(sort.StringSlice(m[ext])))}sort.Sort(sort.Reverse(sort.StringSlice(keys)))default:forext:=rangem{sort.Strings(m[ext])}sort.Strings(keys)}// 3. build a concatenated stringvarliststringfor_,k:=rangekeys{list=fmt.Sprintf("%v\n%v",list,m[k])}returnlist}
Nah, no need for the boilerplate to define a custom sort. This would do sorting the map:
var m = make(map[string][]string)
for _, file := range dir {
if !file.IsDir() {
fileName := file.Name()
ext := strings.Split(fileName, ".")
switch {
case len(ext) > 1:
m[ext[len(ext)-1]] = append(m[ext[len(ext)-1]], fileName)
case len(ext) == 1:
m["no-ext"] = append(m["no-ext"], fileName)
}
}
}
for ext := range m { sort.Strings(m[ext]) }
Grew up in Russia, lived in the States, moved to Germany, sometimes live in Spain. I program since I was 13. I used to program games, maps and now I reverse engineer password managers and other stuff
Location
Berlin and Málaga
Education
MS in CS from State Polytechnic University of St. Petersburg
Yes, I have seen it. Your approach is different by just sorting a list of filenames. The orginal intent is to sort files by extension into different buckets.
Your use of filepath.Ext() is quit clever. Haven't thought of that.
var m = make(map[string][]string)
for _, file := range dir {
if !file.IsDir() {
fileName := file.Name()
ext := filepath.Ext(fileName)
m[ext] = append(m[ext], fileName)
}
}
for ext := range m { sort.Strings(m[ext]) }
Thanks for the replies! filepath.Ext()! Didn't occur to me to try that. It goes to show that the standard library really is pretty complete.
Dirk, I like the for ext := range m { sort.Strings(m[ext]) } solution then I wouldn't need to have a separate sort in each "print" function, it's much clearer that way.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Steve, why do you sort inside the loop on every iteration?
And here's my take on it. You can sort by predicate. It's not exactly very efficient though, since the extension is recalculated every time. But come on, Go could be really annoying sometimes. Look at this verbosity:
In Ruby that would be:
Hah. You caught me! The
sort
should have been moved up into the print function(s) at the very least. It's a bad design decision - I put it there at first just for the sake of simplicity and never got around to cleaning it up. It doesn't really matter in a directory of a few files but would really impact performance in a larger directory. Something like the following might be fine, and still pretty simple to follow.I think I may update the article to make sure it's called out for clarity.
You don't mind?
use it with:
Nah, no need for the boilerplate to define a custom sort. This would do sorting the map:
Edit: sorting the ˋ[]stringˋ within the ˋmap[string][]stringˋ. The map itself can't be sorted.
I don't have a map in my version. I sort an array by a predicate.
Yes, I have seen it. Your approach is different by just sorting a list of filenames. The orginal intent is to sort files by extension into different buckets.
Your use of filepath.Ext() is quit clever. Haven't thought of that.
This would make the example even shorter:
@detunized @dirkolbrich
Thanks for the replies!
filepath.Ext()
! Didn't occur to me to try that. It goes to show that the standard library really is pretty complete.Dirk, I like the
for ext := range m { sort.Strings(m[ext]) }
solution then I wouldn't need to have a separate sort in each "print" function, it's much clearer that way.