From 53713d8c1848d97b991613e12a036add263cf74a Mon Sep 17 00:00:00 2001
From: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
Date: Mon, 7 Feb 2022 15:09:57 +0600
Subject: [PATCH] Channels sotring modifiedOn (#944)

Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
---
 models/contact/src/index.ts |  2 +-
 packages/core/src/query.ts  | 18 ++++++++++++------
 packages/query/src/index.ts |  9 +++++++++
 3 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/models/contact/src/index.ts b/models/contact/src/index.ts
index a3bac20d88..b78a48a47e 100644
--- a/models/contact/src/index.ts
+++ b/models/contact/src/index.ts
@@ -63,7 +63,7 @@ export class TContact extends TDoc implements Contact {
 }
 
 @Model(contact.class.Channel, core.class.AttachedDoc, DOMAIN_CONTACT)
-@UX('Channel' as IntlString, contact.icon.Person)
+@UX('Channel' as IntlString, contact.icon.Person, undefined, 'modifiedOn')
 export class TChannel extends TAttachedDoc implements Channel {
   @Prop(TypeRef(contact.class.ChannelProvider), 'Channel provider' as IntlString)
   provider!: Ref<ChannelProvider>
diff --git a/packages/core/src/query.ts b/packages/core/src/query.ts
index 244c2cc52f..dcf24f12c7 100644
--- a/packages/core/src/query.ts
+++ b/packages/core/src/query.ts
@@ -2,7 +2,7 @@ import { DocumentQuery } from '.'
 import { Doc } from './classes'
 import { getObjectValue } from './objvalue'
 import { createPredicates, isPredicate } from './predicate'
-import { SortingQuery } from './storage'
+import { SortingOrder, SortingQuery } from './storage'
 
 /**
  * @public
@@ -37,27 +37,33 @@ export function resultSort<T extends Doc> (result: T[], sortOptions: SortingQuer
     for (const key in sortOptions) {
       const aValue = getValue(key, a)
       const bValue = getValue(key, b)
-      const result = getSortingResult(aValue, bValue)
-      if (result !== 0) return result * (sortOptions[key] as number)
+      const result = getSortingResult(aValue, bValue, (sortOptions[key] as SortingOrder))
+      if (result !== 0) return result
     }
     return 0
   }
   result.sort(sortFunc)
 }
 
-function getSortingResult (aValue: any, bValue: any): number {
+function getSortingResult (aValue: any, bValue: any, order: SortingOrder): number {
+  let res = 0
   if (typeof aValue === 'undefined') {
     return typeof bValue === 'undefined' ? 0 : -1
   }
   if (typeof bValue === 'undefined') {
     return 1
   }
-  return typeof aValue === 'string' ? aValue.localeCompare(bValue) : (aValue - bValue)
+  if (Array.isArray(aValue) && Array.isArray(bValue)) {
+    res = (aValue.sort((a, b) => (a - b) * order)[0] ?? 0) - (bValue.sort((a, b) => (a - b) * order)[0] ?? 0)
+  } else {
+    res = typeof aValue === 'string' ? aValue.localeCompare(bValue) : (aValue - bValue)
+  }
+  return res * order
 }
 
 function getValue (key: string, obj: any): any {
   let value = getObjectValue(key, obj)
-  if (typeof value === 'object') {
+  if (typeof value === 'object' && !Array.isArray(value)) {
     value = JSON.stringify(value)
   }
   return value
diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts
index b2e2bcff9a..e9ed78f6b4 100644
--- a/packages/query/src/index.ts
+++ b/packages/query/src/index.ts
@@ -253,6 +253,9 @@ export class LiveQuery extends TxProcessor implements Client {
     needCallback = this.proccesLookupUpdateDoc(q.result, lookup, tx)
 
     if (needCallback) {
+      if (q.options?.sort !== undefined) {
+        resultSort(q.result, q.options?.sort)
+      }
       q.callback(this.clone(q.result))
     }
   }
@@ -408,6 +411,9 @@ export class LiveQuery extends TxProcessor implements Client {
     needCallback = this.proccesLookupAddDoc(q.result, lookup, doc)
 
     if (needCallback) {
+      if (q.options?.sort !== undefined) {
+        resultSort(q.result, q.options?.sort)
+      }
       q.callback(this.clone(q.result))
     }
   }
@@ -501,6 +507,9 @@ export class LiveQuery extends TxProcessor implements Client {
       }
     }
     if (needCallback) {
+      if (q.options?.sort !== undefined) {
+        resultSort(q.result, q.options?.sort)
+      }
       q.callback(this.clone(q.result))
     }
   }