Best Gauge code snippet using order.Sort
sort.go
Source:sort.go
...5package elastic67import "errors"89// -- Sorter --1011// Sorter is an interface for sorting strategies, e.g. ScoreSort or FieldSort.12// See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html.13type Sorter interface {14 Source() (interface{}, error)15}1617// -- SortInfo --1819// SortInfo contains information about sorting a field.20type SortInfo struct {21 Sorter22 Field string23 Ascending bool24 Missing interface{}25 IgnoreUnmapped *bool26 SortMode string27 NestedFilter Query28 NestedPath string29}3031func (info SortInfo) Source() (interface{}, error) {32 prop := make(map[string]interface{})33 if info.Ascending {34 prop["order"] = "asc"35 } else {36 prop["order"] = "desc"37 }38 if info.Missing != nil {39 prop["missing"] = info.Missing40 }41 if info.IgnoreUnmapped != nil {42 prop["ignore_unmapped"] = *info.IgnoreUnmapped43 }44 if info.SortMode != "" {45 prop["mode"] = info.SortMode46 }47 if info.NestedFilter != nil {48 src, err := info.NestedFilter.Source()49 if err != nil {50 return nil, err51 }52 prop["nested_filter"] = src53 }54 if info.NestedPath != "" {55 prop["nested_path"] = info.NestedPath56 }57 source := make(map[string]interface{})58 source[info.Field] = prop59 return source, nil60}6162// -- SortByDoc --6364// SortByDoc sorts by the "_doc" field, as described in65// https://www.elastic.co/guide/en/elasticsearch/reference/master/search-request-scroll.html.66//67// Example:68// ss := elastic.NewSearchSource()69// ss = ss.SortBy(elastic.SortByDoc{})70type SortByDoc struct {71 Sorter72}7374// Source returns the JSON-serializable data.75func (s SortByDoc) Source() (interface{}, error) {76 return "_doc", nil77}7879// -- ScoreSort --8081// ScoreSort sorts by relevancy score.82type ScoreSort struct {83 Sorter84 ascending bool85}8687// NewScoreSort creates a new ScoreSort.88func NewScoreSort() ScoreSort {89 return ScoreSort{ascending: false} // Descending by default!90}9192// Order defines whether sorting ascending (default) or descending.93func (s ScoreSort) Order(ascending bool) ScoreSort {94 s.ascending = ascending95 return s96}9798// Asc sets ascending sort order.99func (s ScoreSort) Asc() ScoreSort {100 s.ascending = true101 return s102}103104// Desc sets descending sort order.105func (s ScoreSort) Desc() ScoreSort {106 s.ascending = false107 return s108}109110// Source returns the JSON-serializable data.111func (s ScoreSort) Source() (interface{}, error) {112 source := make(map[string]interface{})113 x := make(map[string]interface{})114 source["_score"] = x115 if s.ascending {116 x["reverse"] = true117 }118 return source, nil119}120121// -- FieldSort --122123// FieldSort sorts by a given field.124type FieldSort struct {125 Sorter126 fieldName string127 ascending bool128 missing interface{}129 ignoreUnmapped *bool130 unmappedType *string131 sortMode *string132 nestedFilter Query133 nestedPath *string134}135136// NewFieldSort creates a new FieldSort.137func NewFieldSort(fieldName string) FieldSort {138 return FieldSort{139 fieldName: fieldName,140 ascending: true,141 }142}143144// FieldName specifies the name of the field to be used for sorting.145func (s FieldSort) FieldName(fieldName string) FieldSort {146 s.fieldName = fieldName147 return s148}149150// Order defines whether sorting ascending (default) or descending.151func (s FieldSort) Order(ascending bool) FieldSort {152 s.ascending = ascending153 return s154}155156// Asc sets ascending sort order.157func (s FieldSort) Asc() FieldSort {158 s.ascending = true159 return s160}161162// Desc sets descending sort order.163func (s FieldSort) Desc() FieldSort {164 s.ascending = false165 return s166}167168// Missing sets the value to be used when a field is missing in a document.169// You can also use "_last" or "_first" to sort missing last or first170// respectively.171func (s FieldSort) Missing(missing interface{}) FieldSort {172 s.missing = missing173 return s174}175176// IgnoreUnmapped specifies what happens if the field does not exist in177// the index. Set it to true to ignore, or set it to false to not ignore (default).178func (s FieldSort) IgnoreUnmapped(ignoreUnmapped bool) FieldSort {179 s.ignoreUnmapped = &ignoreUnmapped180 return s181}182183// UnmappedType sets the type to use when the current field is not mapped184// in an index.185func (s FieldSort) UnmappedType(typ string) FieldSort {186 s.unmappedType = &typ187 return s188}189190// SortMode specifies what values to pick in case a document contains191// multiple values for the targeted sort field. Possible values are:192// min, max, sum, and avg.193func (s FieldSort) SortMode(sortMode string) FieldSort {194 s.sortMode = &sortMode195 return s196}197198// NestedFilter sets a filter that nested objects should match with199// in order to be taken into account for sorting.200func (s FieldSort) NestedFilter(nestedFilter Query) FieldSort {201 s.nestedFilter = nestedFilter202 return s203}204205// NestedPath is used if sorting occurs on a field that is inside a206// nested object.207func (s FieldSort) NestedPath(nestedPath string) FieldSort {208 s.nestedPath = &nestedPath209 return s210}211212// Source returns the JSON-serializable data.213func (s FieldSort) Source() (interface{}, error) {214 source := make(map[string]interface{})215 x := make(map[string]interface{})216 source[s.fieldName] = x217 if s.ascending {218 x["order"] = "asc"219 } else {220 x["order"] = "desc"221 }222 if s.missing != nil {223 x["missing"] = s.missing224 }225 if s.ignoreUnmapped != nil {226 x["ignore_unmapped"] = *s.ignoreUnmapped227 }228 if s.unmappedType != nil {229 x["unmapped_type"] = *s.unmappedType230 }231 if s.sortMode != nil {232 x["mode"] = *s.sortMode233 }234 if s.nestedFilter != nil {235 src, err := s.nestedFilter.Source()236 if err != nil {237 return nil, err238 }239 x["nested_filter"] = src240 }241 if s.nestedPath != nil {242 x["nested_path"] = *s.nestedPath243 }244 return source, nil245}246247// -- GeoDistanceSort --248249// GeoDistanceSort allows for sorting by geographic distance.250// See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_geo_distance_sorting.251type GeoDistanceSort struct {252 Sorter253 fieldName string254 points []*GeoPoint255 geohashes []string256 geoDistance *string257 unit string258 ascending bool259 sortMode *string260 nestedFilter Query261 nestedPath *string262}263264// NewGeoDistanceSort creates a new sorter for geo distances.265func NewGeoDistanceSort(fieldName string) GeoDistanceSort {266 return GeoDistanceSort{267 fieldName: fieldName,268 points: make([]*GeoPoint, 0),269 geohashes: make([]string, 0),270 ascending: true,271 }272}273274// FieldName specifies the name of the (geo) field to use for sorting.275func (s GeoDistanceSort) FieldName(fieldName string) GeoDistanceSort {276 s.fieldName = fieldName277 return s278}279280// Order defines whether sorting ascending (default) or descending.281func (s GeoDistanceSort) Order(ascending bool) GeoDistanceSort {282 s.ascending = ascending283 return s284}285286// Asc sets ascending sort order.287func (s GeoDistanceSort) Asc() GeoDistanceSort {288 s.ascending = true289 return s290}291292// Desc sets descending sort order.293func (s GeoDistanceSort) Desc() GeoDistanceSort {294 s.ascending = false295 return s296}297298// Point specifies a point to create the range distance aggregations from.299func (s GeoDistanceSort) Point(lat, lon float64) GeoDistanceSort {300 s.points = append(s.points, GeoPointFromLatLon(lat, lon))301 return s302}303304// Points specifies the geo point(s) to create the range distance aggregations from.305func (s GeoDistanceSort) Points(points ...*GeoPoint) GeoDistanceSort {306 s.points = append(s.points, points...)307 return s308}309310// GeoHashes specifies the geo point to create the range distance aggregations from.311func (s GeoDistanceSort) GeoHashes(geohashes ...string) GeoDistanceSort {312 s.geohashes = append(s.geohashes, geohashes...)313 return s314}315316// GeoDistance represents how to compute the distance.317// It can be sloppy_arc (default), arc, or plane.318// See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_geo_distance_sorting.319func (s GeoDistanceSort) GeoDistance(geoDistance string) GeoDistanceSort {320 s.geoDistance = &geoDistance321 return s322}323324// Unit specifies the distance unit to use. It defaults to km.325// See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/common-options.html#distance-units326// for details.327func (s GeoDistanceSort) Unit(unit string) GeoDistanceSort {328 s.unit = unit329 return s330}331332// SortMode specifies what values to pick in case a document contains333// multiple values for the targeted sort field. Possible values are:334// min, max, sum, and avg.335func (s GeoDistanceSort) SortMode(sortMode string) GeoDistanceSort {336 s.sortMode = &sortMode337 return s338}339340// NestedFilter sets a filter that nested objects should match with341// in order to be taken into account for sorting.342func (s GeoDistanceSort) NestedFilter(nestedFilter Query) GeoDistanceSort {343 s.nestedFilter = nestedFilter344 return s345}346347// NestedPath is used if sorting occurs on a field that is inside a348// nested object.349func (s GeoDistanceSort) NestedPath(nestedPath string) GeoDistanceSort {350 s.nestedPath = &nestedPath351 return s352}353354// Source returns the JSON-serializable data.355func (s GeoDistanceSort) Source() (interface{}, error) {356 source := make(map[string]interface{})357 x := make(map[string]interface{})358 source["_geo_distance"] = x359360 // Points361 var ptarr []interface{}362 for _, pt := range s.points {363 ptarr = append(ptarr, pt.Source())364 }365 for _, geohash := range s.geohashes {366 ptarr = append(ptarr, geohash)367 }368 x[s.fieldName] = ptarr369370 if s.unit != "" {371 x["unit"] = s.unit372 }373 if s.geoDistance != nil {374 x["distance_type"] = *s.geoDistance375 }376377 if !s.ascending {378 x["reverse"] = true379 }380 if s.sortMode != nil {381 x["mode"] = *s.sortMode382 }383 if s.nestedFilter != nil {384 src, err := s.nestedFilter.Source()385 if err != nil {386 return nil, err387 }388 x["nested_filter"] = src389 }390 if s.nestedPath != nil {391 x["nested_path"] = *s.nestedPath392 }393 return source, nil394}395396// -- ScriptSort --397398// ScriptSort sorts by a custom script. See399// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html#modules-scripting400// for details about scripting.401type ScriptSort struct {402 Sorter403 script *Script404 typ string405 ascending bool406 sortMode *string407 nestedFilter Query408 nestedPath *string409}410411// NewScriptSort creates and initializes a new ScriptSort.412// You must provide a script and a type, e.g. "string" or "number".413func NewScriptSort(script *Script, typ string) ScriptSort {414 return ScriptSort{415 script: script,416 typ: typ,417 ascending: true,418 }419}420421// Type sets the script type, which can be either "string" or "number".422func (s ScriptSort) Type(typ string) ScriptSort {423 s.typ = typ424 return s425}426427// Order defines whether sorting ascending (default) or descending.428func (s ScriptSort) Order(ascending bool) ScriptSort {429 s.ascending = ascending430 return s431}432433// Asc sets ascending sort order.434func (s ScriptSort) Asc() ScriptSort {435 s.ascending = true436 return s437}438439// Desc sets descending sort order.440func (s ScriptSort) Desc() ScriptSort {441 s.ascending = false442 return s443}444445// SortMode specifies what values to pick in case a document contains446// multiple values for the targeted sort field. Possible values are:447// min or max.448func (s ScriptSort) SortMode(sortMode string) ScriptSort {449 s.sortMode = &sortMode450 return s451}452453// NestedFilter sets a filter that nested objects should match with454// in order to be taken into account for sorting.455func (s ScriptSort) NestedFilter(nestedFilter Query) ScriptSort {456 s.nestedFilter = nestedFilter457 return s458}459460// NestedPath is used if sorting occurs on a field that is inside a461// nested object.462func (s ScriptSort) NestedPath(nestedPath string) ScriptSort {463 s.nestedPath = &nestedPath464 return s465}466467// Source returns the JSON-serializable data.468func (s ScriptSort) Source() (interface{}, error) {469 if s.script == nil {470 return nil, errors.New("ScriptSort expected a script")471 }472 source := make(map[string]interface{})473 x := make(map[string]interface{})474 source["_script"] = x475476 src, err := s.script.Source()477 if err != nil {478 return nil, err479 }480 x["script"] = src481482 x["type"] = s.typ483484 if !s.ascending {
...
channel_archive.go
Source:channel_archive.go
...20 chAids, addAids []int6421 arcs map[int64]*arcmdl.Arc22 videos []*model.ChannelArc23 videoMap map[int64]int6424 remainVideos []*model.ChannelArcSort25 ts = time.Now()26 )27 fakeAids = make([]int64, 0)28 if _, _, err = s.channel(c, mid, cid); err != nil {29 log.Error("s.dao.Channel(%d,%d) error(%v)", mid, cid, err)30 return31 }32 if videos, err = s.dao.ChannelVideos(c, mid, cid, false); err != nil {33 log.Error("s.dao.channelVideos(%d,%d) error(%v)", mid, cid, err)34 return35 } else if orderNum = len(videos); orderNum > 0 {36 if len(aids)+orderNum > conf.Conf.Rule.MaxChArcLimit {37 err = ecode.ChMaxArcCount38 return39 }40 videoMap = make(map[int64]int64)41 for _, video := range videos {42 chAids = append(chAids, video.Aid)43 videoMap[video.Aid] = video.Aid44 }45 }46 for _, aid := range aids {47 if _, ok := videoMap[aid]; ok {48 fakeAids = append(fakeAids, aid)49 } else {50 addAids = append(addAids, aid)51 }52 }53 if len(addAids) == 0 {54 err = ecode.ChAidsExist55 return56 }57 if err = s.arcsCheck(c, mid, chAids); err != nil {58 return59 }60 if arcs, err = s.archives(c, addAids); err != nil {61 log.Error("s.arc.Archive3(%v) error(%v)", addAids, err)62 return63 }64 for _, aid := range addAids {65 if arc, ok := arcs[aid]; !ok || !arc.IsNormal() || arc.Author.Mid != mid {66 fakeAids = append(fakeAids, aid)67 continue68 }69 orderNum++70 remainVideos = append(remainVideos, &model.ChannelArcSort{Aid: aid, OrderNum: orderNum})71 }72 if len(remainVideos) == 0 {73 err = ecode.ChAidsExist74 return75 }76 if lastID, err = s.dao.AddChannelArc(c, mid, cid, ts, remainVideos); err != nil {77 log.Error("s.dao.AddChannelArc(mid:%d,cid:%d) error(%v)", mid, cid, err)78 return79 } else if lastID > 0 {80 var arcs []*model.ChannelArc81 for _, v := range remainVideos {82 arc := &model.ChannelArc{ID: lastID, Mid: mid, Cid: cid, Aid: v.Aid, OrderNum: v.OrderNum, Mtime: xtime.Time(ts.Unix())}83 arcs = append(arcs, arc)84 }85 s.dao.AddChannelArcCache(context.Background(), mid, cid, arcs)86 }87 return88}89func (s *Service) arcsCheck(c context.Context, mid int64, aids []int64) (err error) {90 var arcs map[int64]*arcmdl.Arc91 if arcs, err = s.archives(c, aids); err != nil {92 log.Error("s.archives error(%v)", err)93 return94 }95 for _, aid := range aids {96 if arc, ok := arcs[aid]; !ok || !arc.IsNormal() || arc.Author.Mid != mid {97 err = ecode.ChFakeAid98 return99 }100 }101 return102}103// DelChannelArc delete channel archive.104func (s *Service) DelChannelArc(c context.Context, mid, cid, aid int64) (err error) {105 var (106 affected int64107 orderNum int108 videos []*model.ChannelArc109 )110 if videos, err = s.dao.ChannelVideos(c, mid, cid, false); err != nil {111 log.Error("s.dao.Channel(%d,%d) error(%v)", mid, cid, err)112 return113 } else if len(videos) == 0 {114 err = ecode.ChNoArcs115 return116 } else {117 check := false118 for _, video := range videos {119 if aid == video.Aid {120 check = true121 orderNum = video.OrderNum122 }123 }124 if !check {125 err = ecode.ChNoArc126 return127 }128 }129 if affected, err = s.dao.DelChannelArc(c, mid, cid, aid, orderNum); err != nil {130 log.Error("s.dao.DelChannelArc(%d,%d) error(%v)", mid, aid, err)131 return132 } else if affected > 0 {133 s.dao.DelChannelArcCache(c, mid, cid, aid)134 s.setChannelArcSortCache(c, mid, cid)135 }136 return137}138// SortChannelArc sort channel archive.139func (s *Service) SortChannelArc(c context.Context, mid, cid, aid int64, orderNum int) (err error) {140 var (141 videos []*model.ChannelArc142 bfSortBegin, bfSortEnd, chSort, afSort []*model.ChannelArcSort143 affected int64144 aidIndex, aidOn int145 aidCheck bool146 ts = time.Now()147 )148 if videos, err = s.dao.ChannelVideos(c, mid, cid, false); err != nil {149 log.Error("s.dao.ChannelVideos(%d,%d) error(%v)", mid, cid, err)150 return151 } else if len(videos) == 0 {152 err = ecode.ChNoArcs153 return154 } else {155 videoLen := len(videos)156 if orderNum > videoLen {157 err = ecode.RequestErr158 return159 }160 for index, video := range videos {161 if aid == video.Aid {162 aidCheck = true163 aidIndex = index164 aidOn = video.OrderNum165 break166 }167 }168 if !aidCheck {169 err = ecode.RequestErr170 return171 }172 if orderNum > aidOn {173 chSort = append(chSort, &model.ChannelArcSort{Aid: aid, OrderNum: orderNum})174 for i, v := range videos {175 if i < videoLen-orderNum {176 bfSortBegin = append(bfSortBegin, &model.ChannelArcSort{Aid: v.Aid, OrderNum: v.OrderNum})177 } else if i >= videoLen-orderNum && i < aidIndex {178 chSort = append(chSort, &model.ChannelArcSort{Aid: v.Aid, OrderNum: v.OrderNum - 1})179 } else if i > aidIndex {180 bfSortEnd = append(bfSortEnd, &model.ChannelArcSort{Aid: v.Aid, OrderNum: v.OrderNum})181 }182 }183 } else if orderNum < aidOn {184 for i, v := range videos {185 if i < aidIndex {186 bfSortBegin = append(bfSortBegin, &model.ChannelArcSort{Aid: v.Aid, OrderNum: v.OrderNum})187 } else if i > aidIndex && i <= videoLen-orderNum {188 chSort = append(chSort, &model.ChannelArcSort{Aid: v.Aid, OrderNum: v.OrderNum + 1})189 } else if i > videoLen-orderNum {190 bfSortEnd = append(bfSortEnd, &model.ChannelArcSort{Aid: v.Aid, OrderNum: v.OrderNum})191 }192 }193 chSort = append(chSort, &model.ChannelArcSort{Aid: aid, OrderNum: orderNum})194 } else {195 return196 }197 afSort = append(afSort, bfSortBegin...)198 afSort = append(afSort, chSort...)199 afSort = append(afSort, bfSortEnd...)200 }201 if affected, err = s.dao.EditChannelArc(c, mid, cid, ts, chSort); err != nil {202 log.Error("s.dao.s.dao.EditChannelArc(%d,%d,%d,%d) error(%v)", mid, cid, aid, orderNum, err)203 return204 } else if affected > 0 {205 s.dao.SetChannelArcSortCache(c, mid, cid, afSort)206 }207 return208}209// ChannelVideos get channel and channel video info.210func (s *Service) ChannelVideos(c context.Context, mid, cid int64, pn, ps int, isGuest, order bool) (res *model.ChannelDetail, err error) {211 var (212 channel *model.Channel213 start = (pn - 1) * ps214 end = start + ps - 1215 )216 if channel, err = s.Channel(c, mid, cid); err != nil {217 return218 }219 res = &model.ChannelDetail{Channel: channel}220 res.Archives, err = s.channelArc(c, mid, cid, start, end, isGuest, order)221 return222}223func (s *Service) channelVideos(c context.Context, mid, cid int64, start, end int, order bool) (res []*model.ChannelArc, err error) {224 var (225 videos []*model.ChannelArc226 addCache = true227 )228 if res, err = s.dao.ChannelArcsCache(c, mid, cid, start, end, order); err != nil {229 addCache = false230 } else if len(res) > 0 {231 return232 }233 if videos, err = s.dao.ChannelVideos(c, mid, cid, order); err != nil {234 log.Error("s.dao.ChannelVideos(%d,%d) error(%v)", mid, cid, err)235 return236 } else if len(videos) > 0 {237 if addCache {238 s.cache.Do(c, func(c context.Context) {239 s.dao.SetChannelArcsCache(c, mid, cid, videos)240 s.setChannelArcSortCache(c, mid, cid)241 })242 }243 length := len(videos)244 if length < start {245 res = make([]*model.ChannelArc, 0)246 return247 }248 if length > end {249 res = videos[start : end+1]250 } else {251 res = videos[start:]252 }253 }254 return255}256// CheckChannelVideo check useless channel video.257func (s *Service) CheckChannelVideo(c context.Context, mid, cid int64) (err error) {258 var (259 videos []*model.ChannelArc260 aids []int64261 )262 if videos, err = s.dao.ChannelVideos(c, mid, cid, false); err != nil {263 log.Error("s.dao.channelVideos(%d,%d) error(%v)", mid, cid, err)264 return265 }266 for _, v := range videos {267 aids = append(aids, v.Aid)268 }269 err = s.arcsCheck(c, mid, aids)270 return271}272func (s *Service) channelArc(c context.Context, mid, cid int64, start, end int, isGuest, order bool) (res []*arcmdl.Arc, err error) {273 var (274 videoAids []*model.ChannelArc275 archives map[int64]*arcmdl.Arc276 aids []int64277 )278 if videoAids, err = s.channelVideos(c, mid, cid, start, end, order); err != nil {279 log.Error("s.dao.ChannelVideos(%d,%d) error(%v)", mid, cid, err)280 return281 } else if len(videoAids) == 0 {282 res = _emptyChArc283 return284 }285 for _, video := range videoAids {286 aids = append(aids, video.Aid)287 }288 if archives, err = s.archives(c, aids); err != nil {289 log.Error("s.arc.Archives3(%v) error(%v)", aids, err)290 return291 }292 for _, video := range videoAids {293 if arc, ok := archives[video.Aid]; ok {294 if arc.IsNormal() {295 if arc.Access >= 10000 {296 arc.Stat.View = -1297 }298 res = append(res, arc)299 } else {300 res = append(res, &arcmdl.Arc{Aid: video.Aid, Title: arc.Title, Pic: arc.Pic, Stat: arc.Stat, PubDate: arc.PubDate, State: arc.State})301 }302 }303 }304 return305}306func (s *Service) setChannelArcSortCache(c context.Context, mid, cid int64) (err error) {307 var (308 videos []*model.ChannelArc309 sorts []*model.ChannelArcSort310 )311 if videos, err = s.dao.ChannelVideos(c, mid, cid, false); err != nil {312 log.Error("s.dao.ChannelVideos(%d,%d) error(%v)", mid, cid, err)313 return314 } else if len(videos) == 0 {315 return316 }317 for _, v := range videos {318 sort := &model.ChannelArcSort{Aid: v.Aid, OrderNum: v.OrderNum}319 sorts = append(sorts, sort)320 }321 return s.dao.SetChannelArcSortCache(c, mid, cid, sorts)322}323func (s *Service) archives(c context.Context, aids []int64) (archives map[int64]*arcmdl.Arc, err error) {324 var (325 mutex = sync.Mutex{}326 aidsLen = len(aids)327 group, errCtx = errgroup.WithContext(c)328 )329 archives = make(map[int64]*arcmdl.Arc, aidsLen)330 for i := 0; i < aidsLen; i += _aidBulkSize {331 var partAids []int64332 if i+_aidBulkSize > aidsLen {333 partAids = aids[i:]334 } else {335 partAids = aids[i : i+_aidBulkSize]...
addon_test.go
Source:addon_test.go
...47var d = &DummyAddOn{meta: &DummyAddOnMeta{name: "d"}, enabled: true, priority: 0}48var e = &DummyAddOn{meta: &DummyAddOnMeta{name: "e"}, enabled: true, priority: 0}49func Test_sorting_addons_by_priority(t *testing.T) {50 addOns := []AddOn{a, b, c, d, e}51 sort.Sort(ByPriority(addOns))52 expectedSortOrder := "[c d e b a]"53 if fmt.Sprint(addOns) != expectedSortOrder {54 t.Fatal(fmt.Sprintf("Expected sort order '%s', but got '%s'", expectedSortOrder, fmt.Sprint(addOns)))55 }56}57func Test_sorting_addons_by_state_and_name(t *testing.T) {58 addOns := []AddOn{a, b, c, d, e}59 sort.Sort(ByStatusThenName(addOns))60 expectedSortOrder := "[b d e a c]"61 if fmt.Sprint(addOns) != expectedSortOrder {62 t.Fatal(fmt.Sprintf("Expected sort order '%s', but got '%s'", expectedSortOrder, fmt.Sprint(addOns)))63 }64}65func Test_sorting_addons_by_state_priority_and_name(t *testing.T) {66 addOns := []AddOn{a, b, c, d, e}67 sort.Sort(ByStatusThenPriorityThenName(addOns))68 expectedSortOrder := "[d e b c a]"69 if fmt.Sprint(addOns) != expectedSortOrder {70 t.Fatal(fmt.Sprintf("Expected sort order '%s', but got '%s'", expectedSortOrder, fmt.Sprint(addOns)))71 }72}...
Sort
Using AI Code Generation
1import (2func main() {3 a := []int{5, 2, 6, 3, 1, 4}4 sort.Ints(a)5 fmt.Println(a)6}7import (8func main() {9 a := []int{5, 2, 6, 3, 1, 4}10 sort.Sort(sort.Reverse(sort.IntSlice(a)))11 fmt.Println(a)12}13import (14func main() {15 a := []string{"c", "a", "b"}16 sort.Strings(a)17 fmt.Println(a)18}19import (20func main() {21 a := []string{"c", "a", "b"}22 sort.Sort(sort.Reverse(sort.StringSlice(a)))23 fmt.Println(a)24}
Sort
Using AI Code Generation
1import java.util.*;2class Student implements Comparable<Student> {3 int rollno;4 String name;5 int age;6 Student(int rollno, String name, int age) {7 this.rollno = rollno;8 this.name = name;9 this.age = age;10 }11 public int compareTo(Student st) {12 if (age == st.age)13 return 0;14 else if (age > st.age)15 return 1;16 return -1;17 }18}19class Sortbyroll implements Comparator<Student> {20 public int compare(Student a, Student b) {21 return a.rollno - b.rollno;22 }23}24class Sortbyname implements Comparator<Student> {25 public int compare(Student a, Student b) {26 return a.name.compareTo(b.name);27 }28}29public class Example {30 public static void main(String[] args) {31 ArrayList<Student> arraylist = new ArrayList<Student>();32 arraylist.add(new Student(111, "bbbb", 23));33 arraylist.add(new Student(131, "aaaa", 27));34 arraylist.add(new Student(121, "cccc", 21));35 System.out.println("Unsorted");36 for (int i = 0; i < arraylist.size(); i++)37 System.out.println(arraylist.get(i).rollno + " " + arraylist.get(i).name + " " + arraylist.get(i).age);38 Collections.sort(arraylist, new Sortbyroll());39 System.out.println("\nSorted by rollno");40 for (int i = 0; i < arraylist.size(); i++)41 System.out.println(arraylist.get(i).rollno + " " + arraylist.get(i).name + " " + arraylist.get(i).age);42 Collections.sort(arraylist, new Sortbyname());43 System.out.println("\nSorted by name");44 for (int i = 0; i < arraylist.size(); i++)45 System.out.println(arraylist.get(i).rollno + " " + arraylist.get(i).name + " " + arraylist.get(i).age);46 }47}
Sort
Using AI Code Generation
1import (2func main() {3 strs := []string{"c", "a", "b"}4 sort.Strings(strs)5 fmt.Println("Strings:", strs)6 ints := []int{7, 2, 4}7 sort.Ints(ints)8 fmt.Println("Ints:", ints)9 s := sort.IntsAreSorted(ints)10 fmt.Println("Sorted:", s)11}12type Interface interface {13 Len() int14 Less(i, j int) bool15 Swap(i, j int)16}17type Interface interface {18 Len() int19 Less(i, j int) bool20 Swap(i, j int)21}22import (23func main() {24 fruits := []string{"peach", "banana", "kiwi"}25 sort.Strings(fruits)26 fmt.Println("Strings:", fruits)27 ints := []int{7, 2, 4}28 sort.Ints(ints)29 fmt.Println("Ints: ", ints)30 s := sort.IntsAreSorted(ints)31 fmt.Println("Sorted: ", s)32}
Sort
Using AI Code Generation
1import (2func main() {3 slice := []int{5, 2, 6, 3, 1, 4}4 fmt.Println("Original slice: ", slice)5 sort.Ints(slice)6 fmt.Println("Sorted slice: ", slice)7}8Recommended Posts: Go | Sort.SliceStable() Method9Go | Sort.Slice() Method10Go | Sort.Strings() Method11Go | Sort.Sort() Method12Go | Sort.Sort() Method13Go | Sort.SearchStrings() Method14Go | Sort.SearchInts() Method15Go | Sort.Search() Method16Go | Sort.Reverse() Method17Go | Sort.Float64s() Method18Go | Sort.Float64sAreSorted() M
Sort
Using AI Code Generation
1import (2func main() {3 strs := []string{"c", "a", "b"}4 sort.Strings(strs)5 fmt.Println("Strings:", strs)6 ints := []int{7, 2, 4}7 sort.Ints(ints)8 fmt.Println("Ints: ", ints)9 s := sort.IntsAreSorted(ints)10 fmt.Println("Sorted: ", s)11}12import (13func main() {14 strs := []string{"c", "a", "b"}15 sort.Strings(strs)16 fmt.Println("Strings:", strs)17 index := sort.SearchStrings(strs, "b")18 fmt.Println("Index: ", index)19}20import (21func main() {22 strs := []string{"c", "a", "b"}23 sort.Strings(strs)24 fmt.Println("Strings:", strs)25 sort.Sort(sort.Reverse(sort.StringSlice(strs)))26 fmt.Println("Reverse Strings:", strs)27}28import (29type Person struct {30}31func (a ByName) Len() int { return len(a) }32func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }33func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name }34func (a ByAge) Len() int { return len(a) }35func (a ByAge) Swap(i, j int) { a[i], a[j] = a
Sort
Using AI Code Generation
1import java.util.Arrays;2import java.util.Collections;3import java.util.Comparator;4import java.util.List;5import java.util.ArrayList;6public class Order {7 public static void main(String[] args) {8 List<Integer> list = new ArrayList<Integer>();9 list.add(1);10 list.add(2);11 list.add(3);12 list.add(4);13 list.add(5);14 Collections.sort(list, Collections.reverseOrder());15 System.out.println(list);16 }17}18import java.util.Arrays;19import java.util.Collections;20import java.util.Comparator;21import java.util.List;22import java.util.ArrayList;23public class Order {24 public static void main(String[] args) {25 List<Integer> list = new ArrayList<Integer>();26 list.add(1);27 list.add(2);28 list.add(3);29 list.add(4);30 list.add(5);31 Collections.sort(list);32 System.out.println(list);33 }34}35import java.util.Arrays;36import java.util.Collections;37import java.util.Comparator;38import java.util.List;39import java.util.ArrayList;40public class Order {41 public static void main(String[] args) {42 List<Integer> list = new ArrayList<Integer>();43 list.add(1);44 list.add(2);45 list.add(3);46 list.add(4);47 list.add(5);48 Integer[] array = list.toArray(new Integer[0]);49 Arrays.sort(array, Collections.reverseOrder());50 list = Arrays.asList(array);51 System.out.println(list);52 }53}54import java.util.Arrays;55import java.util.Collections;56import java.util.Comparator;57import java.util.List;58import java.util.ArrayList;59public class Order {60 public static void main(String[] args) {61 List<Integer> list = new ArrayList<Integer>();62 list.add(1);63 list.add(2);64 list.add(3
Sort
Using AI Code Generation
1import (2func main() {3 slice := []int{5, 1, 4, 3, 2}4 fmt.Println("Unsorted slice: ", slice)5 sort.Ints(slice)6 fmt.Println("Sorted slice: ", slice)7}8import (9func main() {10 slice := []string{"e", "a", "d", "c", "b"}11 fmt.Println("Unsorted slice: ", slice)12 sort.Strings(slice)13 fmt.Println("Sorted slice: ", slice)14}15import (16type Person struct {17}18func (a ByAge) Len() int { return len(a) }19func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }20func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }21func main() {22 slice := []Person{23 {"e", 5},24 {"a", 1},25 {"d", 4},26 {"c", 3},27 {"b", 2},28 }29 fmt.Println("Unsorted slice: ", slice)30 sort.Sort(ByAge(slice))
Sort
Using AI Code Generation
1import (2func main() {3 unsorted := []int{2, 4, 1, 3, 5}4 fmt.Println("Unsorted:", unsorted)5 sort.Ints(unsorted)6 fmt.Println("Sorted:", unsorted)7}8import (9func main() {10 unsorted := []int{2, 4, 1, 3, 5}11 fmt.Println("Unsorted:", unsorted)12 sort.Sort(sort.Reverse(sort.IntSlice(unsorted)))13 fmt.Println("Sorted:", unsorted)14}15import (16func main() {17 unsorted := []int{2, 4, 1, 3, 5}18 fmt.Println("Unsorted:", unsorted)19 sort.Sort(sort.IntSlice(unsorted))20 fmt.Println("Sorted:", unsorted)21}22import (23func main() {24 unsorted := []int{2, 4, 1, 3, 5}25 fmt.Println("Unsorted:", unsorted)26 sort.Sort(sort.Reverse(sort.IntSlice(unsorted)))27 fmt.Println("Sorted:", unsorted)28}29import (30func main() {31 unsorted := []int{2, 4, 1, 3, 5}32 fmt.Println("Unsorted:", unsorted)33 sort.Sort(sort.IntSlice(unsorted))34 fmt.Println("Sorted:", unsorted)35}36import (
Sort
Using AI Code Generation
1import (2func main() {3 words := []string{"zebra", "apple", "cat", "banana"}4 sort.Strings(words)5 fmt.Println("Words: ", words)6}7import (8func main() {9 numbers := []int{10, 5, 8, 9, 3}10 sort.Ints(numbers)11 fmt.Println("Numbers: ", numbers)12}13import (14func main() {15 numbers := []float64{10.5, 5.6, 8.9, 9.2, 3.1}16 sort.Float64s(numbers)17 fmt.Println("Numbers: ", numbers)18}19import (20func main() {21 words := []string{"zebra", "apple", "cat", "banana"}22 sort.Sort(sort.Reverse(sort.StringSlice(words)))23 fmt.Println("Words: ", words)24}25import (26func main() {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!