Browse Source

cdb sort by column

trethaller 4 years ago
parent
commit
f48ad86d82
2 changed files with 35 additions and 0 deletions
  1. 6 0
      hide/comp/cdb/Editor.hx
  2. 29 0
      hide/comp/cdb/Table.hx

+ 6 - 0
hide/comp/cdb/Editor.hx

@@ -790,6 +790,12 @@ class Editor extends Component {
 				endChanges();
 				endChanges();
 				refresh();
 				refresh();
 			})});
 			})});
+
+			switch(col.type) {
+			case TId | TString: 
+				menu.push({ label : "Sort", click: () -> table.sortBy(col) });
+			default:
+			}
 		}
 		}
 
 
 		if( col.type == TString && col.kind == Script )
 		if( col.type == TString && col.kind == Script )

+ 29 - 0
hide/comp/cdb/Table.hx

@@ -391,6 +391,35 @@ class Table extends Component {
 		return false;
 		return false;
 	}
 	}
 
 
+	public function sortBy(col: cdb.Data.Column) {
+		editor.beginChanges();
+		var group : Array<Dynamic> = [];
+		var startIndex = 0;
+		function sort() {
+			group.sort(function(a, b) {
+				var val1 = Reflect.field(a, col.name);
+				var val2 = Reflect.field(b, col.name);
+				return Reflect.compare(val1, val2);
+			});
+			for(i in 0...group.length)
+				sheet.lines[startIndex + i] = group[i];
+		}
+
+		for(i in 0...lines.length) {
+			var sep = sheet.separators.indexOf(i) >= 0;
+			if(sep) {
+				sort();
+				group = [];
+				startIndex = i;
+			}
+			group.push(lines[i].obj);
+		}
+		sort();
+
+		editor.endChanges();
+		refresh();
+	}
+
 	function toggleList( cell : Cell, ?immediate : Bool, ?make : Void -> SubTable ) {
 	function toggleList( cell : Cell, ?immediate : Bool, ?make : Void -> SubTable ) {
 		var line = cell.line;
 		var line = cell.line;
 		var cur = line.subTable;
 		var cur = line.subTable;