penguin/golifehk

@golifehk bot in Telegram

commit 55d4ac4adc81e341441bfba21bf7204e327a5441

author斟酌 鵬兄 <tgckpg@gmail.com>
date2022-11-14T16:22:39Z
subjectMTR: Sort schedules by route
commit 55d4ac4adc81e341441bfba21bf7204e327a5441
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2022-11-14T16:22:39Z

    MTR: Sort schedules by route
---
 datasources/mtr/bus/BusStop.go     | 13 +++++++++++++
 datasources/mtr/bus/QueryResult.go | 13 ++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/datasources/mtr/bus/BusStop.go b/datasources/mtr/bus/BusStop.go
index 20af3c9..cd8a400 100644
--- a/datasources/mtr/bus/BusStop.go
+++ b/datasources/mtr/bus/BusStop.go
@@ -47,3 +47,16 @@ func ( this *BusStop ) Reload() {
     this.Key = &this.RouteId
     this.SearchData = &searchData
 }
+
+type ByRoute [] *BusStop
+
+func (a ByRoute) Len() int           { return len(a) }
+func (a ByRoute) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
+func (a ByRoute) Less(i, j int) bool {
+    _a := *a[i]
+    _b := *a[j]
+    if _a.RouteId == _b.RouteId {
+        return _a.Direction < _b.Direction
+    }
+    return _a.RouteId < _b.RouteId
+}
diff --git a/datasources/mtr/bus/QueryResult.go b/datasources/mtr/bus/QueryResult.go
index 482663d..fa58ed4 100644
--- a/datasources/mtr/bus/QueryResult.go
+++ b/datasources/mtr/bus/QueryResult.go
@@ -115,7 +115,18 @@ func ( this QueryResult ) Message() ( string, error ) {
         }
     } else {
         if 0 < len( *this.Schedules ) {
-            for busStop, buses := range *this.Schedules {
+
+            busStops := [] *BusStop{}
+
+            for b, _ := range *this.Schedules {
+                busStops = append( busStops, b )
+            }
+
+            sort.Sort( ByRoute( busStops ) )
+
+            for _, busStop := range busStops {
+                buses := (*this.Schedules)[ busStop ]
+
                 writeShortRoute( &this.Lang, &sb, busStop )
                 for _, bus := range buses.Buses {
                     sb.WriteString( "  \\* " )