Article View: gnu.emacs.bug
Article #99638Modified sort.el to sort on floating point fields
From: Skip Montanaro
Date: Wed, 20 Jun 1990 13:31
Date: Wed, 20 Jun 1990 13:31
87 lines
3451 bytes
3451 bytes
Here are context diffs for sort.el (GNU Emacs Version 18.55) to sort on
floating point numeric fields. Note that I have three autoloads for the
various routines used from float.el, since that file doesn't (provide
'float). That omission from float.el is a bug and should be fixed.
Skip (montanaro@crdgw1.ge.com)
cd /home/kreskin/u0/montnaro/emacs/
diff -c2 sort.el sort-float.el
*** sort.el Mon May 30 17:53:34 1988
--- sort-float.el Wed Jun 20 09:29:46 1990
***************
*** 21,24 ****
--- 21,28 ----
(provide 'sort)
+ (autoload 'string-to-float "float")
+ (autoload 'floatp "float")
+ (autoload 'f< "float")
+
;; Original version of most of this contributed by Howie Kaye
***************
*** 66,70 ****
(if (fboundp 'sortcar)
(sortcar sort-lists
! (cond ((numberp (car (car sort-lists)))
'<)
((consp (car (car sort-lists)))
--- 70,76 ----
(if (fboundp 'sortcar)
(sortcar sort-lists
! (cond ((floatp (car (car sort-lists)))
! 'f<)
! ((numberp (car (car sort-lists)))
'<)
((consp (car (car sort-lists)))
***************
*** 73,79 ****
'string<)))
(sort sort-lists
! (cond ((numberp (car (car sort-lists)))
(function
(lambda (a b)
(< (car a) (car b)))))
((consp (car (car sort-lists)))
--- 79,89 ----
'string<)))
(sort sort-lists
! (cond ((floatp (car (car sort-lists)))
(function
(lambda (a b)
+ (f< (car a) (car b)))))
+ ((numberp (car (car sort-lists)))
+ (function
+ (lambda (a b)
(< (car a) (car b)))))
((consp (car (car sort-lists)))
***************
*** 343,344 ****
--- 353,375 ----
(concat "+0." col-start)
(concat "-0." col-end)))))
+
+ (defun sort-float-fields (field beg end)
+ "Sort lines in region numerically by the ARGth field of each line.
+ Fields are separated by whitespace and numbered from 1 up. Specified field
+ must contain a floating point number in each line of the region. With a
+ negative arg, sorts by the -ARG'th field, in reverse order. Called from a
+ program, there are three arguments: FIELD, BEG and END. BEG and END specify
+ region to sort."
+ (interactive "p\nr")
+ (sort-fields-1 field beg end
+ (function (lambda ()
+ (sort-skip-fields (1- field))
+ (string-to-float
+ (buffer-substring
+ (point)
+ (save-excursion
+ (skip-chars-forward
+ "[+-]?[0-9]*\.?[0-9]*\\([eE][+-]?[0-9]+\\)?")
+ (point))))))
+ nil))
+
Compilation finished at Wed Jun 20 09:29:51
Technical Details
Message-ID:
<9006201331.AA13684@moose.crd.Ge.Com>Path:
arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!wuarchive!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!moose.crd.ge.com!montnaroShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fb2f1,29efa16cc329c99b,start X-Google-Attributes: gidfb2f1,public X-Google-ArrivalTime: 1990-06-20 06:31:57 PST Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!wuarchive!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!moose.crd.ge.com!montnaro From: montnaro@moose.crd.ge.com (Skip Montanaro) Newsgroups: gnu.emacs.bug Subject: Modified sort.el to sort on floating point fields Message-ID: <9006201331.AA13684@moose.crd.Ge.Com> Date: 20 Jun 90 13:31:57 GMT Sender: daemon@tut.cis.ohio-state.edu Reply-To: montanaro@crdgw1.ge.com (Skip Montanaro) Distribution: gnu Organization: GNUs Not Usenet Lines: 87 Posted: Wed Jun 20 14:31:57 1990