Skip to content

Commit

Permalink
Sort by priority
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Mar 25, 2021
1 parent fa2ee30 commit c53fb9a
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"sort"

"github.com/acarl005/stripansi"
todoist "github.com/sachaos/todoist/lib"
Expand All @@ -22,20 +23,6 @@ func traverseItems(item *todoist.Item, f func(item *todoist.Item, depth int), de
}
}

func sortItems(itemListPtr *[][]string, byIndex int) {
itemList := *itemListPtr
length := len(itemList)
for i := 0; i < length-1; i++ {
for j := 0; j < length-1-i; j++ {
if stripansi.Strip(itemList[j][byIndex]) > stripansi.Strip(itemList[j+1][byIndex]) {
tmp := itemList[j]
itemList[j] = itemList[j+1]
itemList[j+1] = tmp
}
}
}
}

type TaskJSON struct {
ID string `json:"ID"`
Priority string `json:"Priority"`
Expand Down Expand Up @@ -87,12 +74,13 @@ func List(c *cli.Context) error {
jsonObjects = append(jsonObjects, obj)
}, 0)

// FIXME: sort JSON objects
// if c.Bool("priority") == true {
// // sort output by priority
// // and no need to use "else block" as items returned by API are already sorted by task id
// sortItems(&itemList, 1)
// }
if c.Bool("priority") {
// sort output by priority
// and no need to use "else block" as items returned by API are already sorted by task id
sort.Slice(jsonObjects, func(i, j int) bool {
return stripansi.Strip(jsonObjects[i].Priority) < stripansi.Strip(jsonObjects[j].Priority)
})
}

defer writer.Flush()

Expand Down

0 comments on commit c53fb9a

Please sign in to comment.