本文整理汇总了Golang中sort.Float64Slice.Sort方法的典型用法代码示例。如果您正苦于以下问题:Golang Float64Slice.Sort方法的具体用法?Golang Float64Slice.Sort怎么用?Golang Float64Slice.Sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sort.Float64Slice
的用法示例。
在下文中一共展示了Float64Slice.Sort方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
var numbers sort.Float64Slice = []float64{9, 2, 12, 1, 16, 12, 10}
numbers.Sort() //numbers are sorted and can be used as is.
result := median(numbers)
fmt.Println(result)
}
示例2: CalculateEvenRanges2
// CalculateEvenRanges2 genetare even diapasons of ranges
func CalculateEvenRanges2(list []float32, rangesCount int) ([]float64, error) {
if rangesCount < 4 || rangesCount%2 == 1 {
return nil, errors.New("the ranges count must be positive even value more than 3")
}
if list == nil || len(list) < rangesCount {
return nil, errors.New("quantity of set float32 values must be more than the ranges count and more than 3")
}
var sortedB sort.Float64Slice
var sortedL sort.Float64Slice
var totalB float64
var totalL float64
for i := 0; i < len(list)-1; i++ {
delta := float64(list[i+1] / list[i])
if delta == 1 {
continue
} else {
if delta > 1 {
totalB += delta - 1
sortedB = append(sortedB, delta-1)
} else {
totalL += 1 - delta
sortedL = append(sortedL, 1-delta)
}
}
}
sortedB.Sort()
sortedL.Sort()
half := rangesCount / 2
resultB := evenHalfRanges2(half, totalB, sortedB)
resultL := evenHalfRanges2(half, totalL, sortedL)
var ranges = make([]float64, rangesCount-1)
lenghtL := len(resultL)
for i := 1; i <= lenghtL; i++ {
ranges[i-1] = 1 - resultL[lenghtL-i]
}
ranges[lenghtL] = 1
for i := 0; i < len(resultB); i++ {
ranges[lenghtL+1+i] = 1 + resultB[i]
}
return ranges, nil
}
示例3: CalculateRanges
// CalculateRanges genetare diapason of ranges
func CalculateRanges(list []float32, rangesCount int) ([]float64, error) {
if rangesCount < 1 {
return nil, errors.New("the ranges count must be positive value more than 1")
}
if list == nil || len(list) < rangesCount {
return nil, errors.New("quantity of set float32 values must be more than the ranges count and more than 1")
}
// var deltas []float32
var sorted sort.Float64Slice
var total float64
for i := 0; i < len(list)-1; i++ {
delta := float64(list[i+1] / list[i])
total += delta
// deltas = append(deltas, delta)
sorted = append(sorted, delta)
}
sorted.Sort()
step := total / float64(rangesCount)
var ranges = make([]float64, rangesCount-1)
check := false
for i := 1; i < rangesCount; i++ {
level := step * float64(i)
var sum float64
var prev float64
for _, element := range sorted {
sum += element
if sum > level {
ranges[i-1] = (element + prev) / 2
if i > 1 && ranges[i-1] != ranges[i-2] {
check = true
}
break
} else {
prev = element
}
}
}
if check {
return ranges, nil
}
return nil, errors.New("CalculateRanges errer: ranges not vialid")
}
示例4: CalculateEvenRanges
// CalculateEvenRanges genetare even diapasons of ranges
func CalculateEvenRanges(list []float32, rangesCount int) ([]float64, error) {
if rangesCount < 1 || rangesCount%2 == 1 {
return nil, errors.New("the ranges count must be positive even value more than 1")
}
if list == nil || len(list) < rangesCount {
return nil, errors.New("quantity of set float32 values must be more than the ranges count and more than 1")
}
var sortedB sort.Float64Slice
var sortedL sort.Float64Slice
var totalB float64
var totalL float64
for i := 0; i < len(list)-1; i++ {
delta := float64(list[i+1] / list[i])
if delta == 1 {
continue
} else {
if delta > 1 {
totalB += delta
sortedB = append(sortedB, delta)
} else {
totalL += delta
sortedL = append(sortedL, delta)
}
}
}
sortedB.Sort()
sortedL.Sort()
half := rangesCount / 2
// Hight & Low
rangesB, checkB := evenHalfRanges(half, totalB, sortedB)
rangesL, checkL := evenHalfRanges(half, totalL, sortedL)
if checkB && checkL {
var ranges = make([]float64, rangesCount-1)
copy(ranges[0:half-1], rangesL)
ranges[half] = 1.0
copy(ranges[half+1:rangesCount-2], rangesB)
return ranges, nil
}
return nil, errors.New("CalculateEvenRanges errer: ranges not vialid")
}
示例5: topNTrades
func (a *Account) topNTrades(n int64, normalSortOrder bool) []*orders.Order {
fslice := sort.Float64Slice{}
for e := a.Orders.Front(); e != nil; e = e.Next() {
order := e.Value.(*orders.Order)
fslice = append(fslice, order.Profit())
}
if normalSortOrder {
sort.Sort(sort.Reverse(fslice))
} else {
fslice.Sort()
}
trades := []*orders.Order{}
count := int64(0)
for _, profit := range fslice {
if count >= n {
break
}
for e := a.Orders.Front(); e != nil; e = e.Next() {
order := e.Value.(*orders.Order)
if order.Profit() == profit {
trades = append(trades, order)
break
}
}
count++
}
return trades
}
示例6: GetLCR
func (cd *CallDescriptor) GetLCR(stats StatsInterface, p *utils.Paginator) (*LCRCost, error) {
cd.account = nil // make sure it's not cached
lcr, err := cd.GetLCRFromStorage()
if err != nil {
return nil, err
}
// sort by activation time
lcr.Sort()
// find if one ore more entries apply to this cd (create lcr timespans)
// create timespans and attach lcr entries to them
lcrCost := &LCRCost{}
for _, lcrActivation := range lcr.Activations {
//log.Printf("Activation: %+v", lcrActivation)
lcrEntry := lcrActivation.GetLCREntryForPrefix(cd.Destination)
//log.Printf("Entry: %+v", lcrEntry)
if lcrActivation.ActivationTime.Before(cd.TimeStart) ||
lcrActivation.ActivationTime.Equal(cd.TimeStart) {
lcrCost.Entry = lcrEntry
} else {
// because lcr is sorted the folowing ones will
// only activate later than cd.Timestart
break
}
}
if lcrCost.Entry == nil {
return lcrCost, nil
}
//log.Printf("Entry: %+v", lcrCost.Entry)
if lcrCost.Entry.Strategy == LCR_STRATEGY_STATIC {
for _, supplier := range lcrCost.Entry.GetParams() {
lcrCD := cd.Clone()
lcrCD.Account = supplier
lcrCD.Subject = supplier
lcrCD.Category = lcrCost.Entry.RPCategory
fullSupplier := utils.ConcatenatedKey(lcrCD.Direction, lcrCD.Tenant, lcrCD.Category, lcrCD.Subject)
var cc *CallCost
var err error
if cd.account, err = accountingStorage.GetAccount(lcrCD.GetAccountKey()); err == nil {
if cd.account.Disabled {
lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{
Supplier: fullSupplier,
Error: fmt.Sprintf("supplier %s is disabled", supplier),
})
continue
}
cc, err = lcrCD.debit(cd.account, true, true)
} else {
cc, err = lcrCD.GetCost()
}
//log.Printf("CC: %+v", cc.Timespans[0].ratingInfo.RateIntervals[0].Rating.Rates[0])
if err != nil || cc == nil {
lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{
Supplier: fullSupplier,
Error: err.Error(),
})
} else {
lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{
Supplier: fullSupplier,
Cost: cc.Cost,
Duration: cc.GetDuration(),
})
}
}
} else {
// find rating profiles
category := lcrCost.Entry.RPCategory
if category == utils.META_DEFAULT {
category = lcr.Category
}
ratingProfileSearchKey := utils.ConcatenatedKey(lcr.Direction, lcr.Tenant, lcrCost.Entry.RPCategory)
//log.Print("KEY: ", ratingProfileSearchKey)
suppliers := cache2go.GetEntriesKeys(utils.RATING_PROFILE_PREFIX + ratingProfileSearchKey)
for _, supplier := range suppliers {
//log.Print("Supplier: ", supplier)
split := strings.Split(supplier, ":")
supplier = split[len(split)-1]
lcrCD := cd.Clone()
lcrCD.Category = category
lcrCD.Account = supplier
lcrCD.Subject = supplier
fullSupplier := utils.ConcatenatedKey(lcrCD.Direction, lcrCD.Tenant, lcrCD.Category, lcrCD.Subject)
var qosSortParams []string
var asrValues sort.Float64Slice
var pddValues sort.Float64Slice
var acdValues sort.Float64Slice
var tcdValues sort.Float64Slice
var accValues sort.Float64Slice
var tccValues sort.Float64Slice
var ddcValues sort.Float64Slice
// track if one value is never calculated
asrNeverConsidered := true
pddNeverConsidered := true
acdNeverConsidered := true
tcdNeverConsidered := true
accNeverConsidered := true
tccNeverConsidered := true
ddcNeverConsidered := true
if utils.IsSliceMember([]string{LCR_STRATEGY_QOS, LCR_STRATEGY_QOS_THRESHOLD, LCR_STRATEGY_LOAD}, lcrCost.Entry.Strategy) {
if stats == nil {
//.........这里部分代码省略.........