 (defun ledger-sort-region (beg end)
   "Sort the region from BEG to END in chronological order."
   (interactive "r") ;; load beg and end from point and mark
-		    ;; automagically
+	;; automagically
   (let ((new-beg beg)
-	(new-end end))
-    (setq inhibit-modification-hooks t)
+				(new-end end)
+				point-delta
+				(bounds (ledger-find-xact-extents (point)))
+				target-xact)
+
+		(setq point-delta (- (point) (car bounds)))
+		(setq target-xact (buffer-substring (car bounds) (cadr bounds)))
+		(setq inhibit-modification-hooks t)
     (save-excursion 
       (save-restriction
-	(goto-char beg)
-	(ledger-next-record-function) ;; make sure point is at the
-				      ;; beginning of a xact
-	(setq new-beg (point))
-	(goto-char end)
-	(ledger-next-record-function) ;; make sure end of region is at
-				      ;; the beginning of next record
-				      ;; after the region
-	(setq new-end (point))
-	(narrow-to-region new-beg new-end)
-	(goto-char new-beg)
+				(goto-char beg)
+				(ledger-next-record-function) ;; make sure point is at the
+				;; beginning of a xact
+				(setq new-beg (point))
+				(goto-char end)
+				(ledger-next-record-function) ;; make sure end of region is at
+				;; the beginning of next record
+				;; after the region
+				(setq new-end (point))
+				(narrow-to-region new-beg new-end)
+				(goto-char new-beg)
 
-	(let ((inhibit-field-text-motion t))
-	  (sort-subr
-	   nil
-	   'ledger-next-record-function
-	   'ledger-end-record-function
-	   'ledger-sort-startkey))))
+				(let ((inhibit-field-text-motion t))
+					(sort-subr
+					 nil
+					 'ledger-next-record-function
+					 'ledger-end-record-function
+					 'ledger-sort-startkey))))
+
+		(goto-char beg)
+		(re-search-forward (regexp-quote target-xact))
+		(goto-char (+ (match-beginning 0) point-delta))
     (setq inhibit-modification-hooks nil)))
 
 (defun ledger-sort-buffer ()
   "Sort the entire buffer."
   (interactive)
-  (goto-char (point-min))
-  (let ((sort-start (ledger-sort-find-start))
-	(sort-end (ledger-sort-find-end)))
+  (let (sort-start 
+				sort-end)
+		(save-excursion
+			(goto-char (point-min))
+			(setq sort-start (ledger-sort-find-start)
+						sort-end (ledger-sort-find-end)))
     (ledger-sort-region (if sort-start
-			    sort-start
-			    (point-min)) 
-			(if sort-end
-			    sort-end
-			    (point-max)))))
+														sort-start
+														(point-min)) 
+												(if sort-end
+														sort-end
+														(point-max)))))
 
 (provide 'ldg-sort)
 

  (save-excursion
    (goto-char (point-min))
    (if (ledger-sort-find-end)
	(delete-region (match-beginning 0) (match-end 0))))
  (beginning-of-line)
  (insert "\n; Ledger-mode: End sort\n\n"))


  (save-excursion
    (goto-char (point-min))
    (if (ledger-sort-find-start)
				(delete-region (match-beginning 0) (match-end 0))))
  (beginning-of-line)
  (insert "\n; Ledger-mode: Start sort\n\n"))

