From 32685cc367b86c81df3f025f63261aa91ea3cd8c Mon Sep 17 00:00:00 2001 From: horus_arch Date: Fri, 9 Sep 2016 12:25:41 +0200 Subject: Fix sorting. --- intercept.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/intercept.go b/intercept.go index eeea651..3be3ddf 100644 --- a/intercept.go +++ b/intercept.go @@ -85,8 +85,30 @@ func (t *TemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func execTemplate(w http.ResponseWriter, r *http.Request, data string) { - links := getToken(data) - sort.Sort(LinksAsSlice(links)) // see sort.go + // We get all links in the document + allLinks := getToken(data) + + // all directories + var dirs []Link + // all files + var links []Link + + // we split the file and directory links + for k, v := range allLinks { + if allLinks[k].IsDir { + dirs = append(dirs, v) + } else { + links = append(links, v) + } + } + // sort them independent + sort.Sort(LinksAsSlice(dirs)) + sort.Sort(LinksAsSlice(links)) + + // merge them back + allLinks = append(dirs, links...) + + // ... now directories are listed first tmpl := template.New("page") tmpl, err := tmpl.Parse(getTemplate()) @@ -102,7 +124,7 @@ func execTemplate(w http.ResponseWriter, r *http.Request, data string) { URL string Favicon template.HTML AllowUpload bool - }{Links: links, URL: r.URL.Path, Favicon: getFavicon(), AllowUpload: _allow_upload}) + }{Links: allLinks, URL: r.URL.Path, Favicon: getFavicon(), AllowUpload: _allow_upload}) if err != nil { log.Println(err) } -- cgit v1.2.3