Added a generic task sorter utility. This allows for an array of tasks to be sorted based on any resource. The possible resources by which an array of tasks can be sorted currently are listed in def/sortingCriteria.go. One can add more to this if required.
This commit is contained in:
parent
9b9dc73269
commit
f29b7f51a9
2 changed files with 44 additions and 0 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/mdesenfants/gokmeans"
|
||||
"sort"
|
||||
"log"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Information about a cluster of tasks
|
||||
|
@ -114,3 +115,17 @@ func labelAndOrder(clusters map[int][]Task, numberOfClusters int, taskObservatio
|
|||
})
|
||||
return sizedClusters
|
||||
}
|
||||
|
||||
// Generic Task Sorter.
|
||||
// Be able to sort an array of tasks based on any of the tasks' resources.
|
||||
|
||||
// Retrieve a sorter (same signature as 'Less' function in sort.Interface) for the given sorting criteria.
|
||||
func TaskSorter(sc SortCriteria, tasks []Task) func (i, j int) bool {
|
||||
return func (i, j int) bool {
|
||||
taskIFields := reflect.Indirect(reflect.ValueOf(tasks[i]))
|
||||
tasksJFields := reflect.Indirect(reflect.ValueOf(tasks[j]))
|
||||
resourceI := taskIFields.FieldByName(sc.String()).Float()
|
||||
resourceJ := tasksJFields.FieldByName(sc.String()).Float()
|
||||
return resourceI <= resourceJ
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue