Skip to content

Commit

Permalink
ordering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nocodeleaks committed May 21, 2024
1 parent 64241ac commit 2927de0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/controllers/api_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func GetTimestamp(timestamp string) (result int64, err error) {
func GetMessages(server *models.QpWhatsappServer, timestamp int64) (messages []whatsapp.WhatsappMessage) {
searchTime := time.Unix(timestamp, 0)
messages = server.GetMessages(searchTime)
sort.Sort(whatsapp.ByTimestamp(messages))
sort.Sort(whatsapp.WhatsappOrderedMessages(messages))
return
}

Expand Down
3 changes: 3 additions & 0 deletions src/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@
"filelength",
"forcedfalse",
"forcedtrue",
"forwardingscore",
"forwardinternal",
"frominternal",
"fromme",
"gambiarra",
"godotenv",
"handlereadreceipt",
Expand Down
14 changes: 10 additions & 4 deletions src/whatsapp/whatsapp_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ type WhatsappMessage struct {

//region ORDER BY TIMESTAMP

type ByTimestamp []WhatsappMessage
// Ordering by (Timestamp) and then (Id)
type WhatsappOrderedMessages []WhatsappMessage

func (m ByTimestamp) Len() int { return len(m) }
func (m ByTimestamp) Less(i, j int) bool { return m[i].Timestamp.After(m[j].Timestamp) }
func (m ByTimestamp) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m WhatsappOrderedMessages) Len() int { return len(m) }
func (m WhatsappOrderedMessages) Less(i, j int) bool {
if m[i].Timestamp == m[j].Timestamp {
return m[i].Id < m[j].Id
}
return m[i].Timestamp.After(m[j].Timestamp)
}
func (m WhatsappOrderedMessages) Swap(i, j int) { m[i], m[j] = m[j], m[i] }

//endregion

Expand Down

0 comments on commit 2927de0

Please sign in to comment.