From ea73082dc0293a0bafde20b0f82d674b7ca230f9 Mon Sep 17 00:00:00 2001
From: mehekj <mehek.jethani@gmail.com>
Date: Mon, 30 Jan 2023 22:14:52 -0500
Subject: basic sorting

---
 .../collectionSchema/CollectionSchemaView.tsx      | 32 ++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

(limited to 'src/client/views/collections/collectionSchema/CollectionSchemaView.tsx')

diff --git a/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx b/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx
index 29b22c0d5..4fb3f68c1 100644
--- a/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx
+++ b/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx
@@ -1,12 +1,12 @@
 import React = require('react');
 import { action, computed, observable, ObservableMap, ObservableSet, untracked } from 'mobx';
 import { observer } from 'mobx-react';
-import { Doc, DocListCast, Opt } from '../../../../fields/Doc';
+import { Doc, DocListCast, Field, Opt } from '../../../../fields/Doc';
 import { Id } from '../../../../fields/FieldSymbols';
 import { List } from '../../../../fields/List';
 import { RichTextField } from '../../../../fields/RichTextField';
 import { listSpec } from '../../../../fields/Schema';
-import { BoolCast, Cast, StrCast } from '../../../../fields/Types';
+import { BoolCast, Cast, NumCast, StrCast } from '../../../../fields/Types';
 import { ImageField } from '../../../../fields/URLField';
 import { emptyFunction, returnEmptyString, setupMoveUpEvents, Utils } from '../../../../Utils';
 import { Docs, DocUtils } from '../../../documents/Documents';
@@ -89,6 +89,31 @@ export class CollectionSchemaView extends CollectionSubView() {
         return this._displayColumnWidths ?? this.storedColumnWidths;
     }
 
+    @computed get sortField() {
+        return StrCast(this.layoutDoc.sortField, 'creationDate');
+    }
+
+    @computed get sortDesc() {
+        return BoolCast(this.layoutDoc.sortDesc);
+    }
+
+    @undoBatch
+    @action
+    setSort = (field: string, desc: boolean) => {
+        this.layoutDoc.sortField = field;
+        this.layoutDoc.sortDesc = desc;
+
+        this.childDocs.sort((docA, docB) => {
+            const aStr = Field.toString(docA[field] as Field);
+            const bStr = Field.toString(docB[field] as Field);
+            var out = 0;
+            if (aStr < bStr) out = -1;
+            if (aStr > bStr) out = 1;
+            if (desc) out *= -1;
+            return out;
+        });
+    };
+
     @undoBatch
     @action
     changeColumnKey = (index: number, newKey: string, defaultVal?: any) => {
@@ -430,6 +455,9 @@ export class CollectionSchemaView extends CollectionSubView() {
                                     columnKeys={this.columnKeys}
                                     columnWidths={this.displayColumnWidths}
                                     possibleKeys={this.documentKeys}
+                                    sortField={this.sortField}
+                                    sortDesc={this.sortDesc}
+                                    setSort={this.setSort}
                                     changeColumnKey={this.changeColumnKey}
                                     addColumn={this.addColumn}
                                     removeColumn={this.removeColumn}
-- 
cgit v1.2.3-70-g09d2

