Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to sort words in a line

111 views
Skip to first unread message

Rainer Stengele

unread,
Jul 17, 2007, 6:01:59 AM7/17/07
to
hi,

I just couldn't find a fast solution to sort a line of words:

zzz aaa hhhh

-->

aaa hhhh zzz


Did I miss a simple command?

thanx.

Pascal Bourguignon

unread,
Jul 17, 2007, 6:26:13 AM7/17/07
to

AFAIK, no.

But it's rather simple a command to write:

(defun sort-words-in-lines (start end)
(interactive "r")
(goto-char start)
(beginning-of-line)
(while (< (setq start (point)) end)
(let ((words (sort (split-string (buffer-substring start (line-end-position)))
(function string-lessp))))
(delete-region start (line-end-position))
(dolist (word words ) (insert word " ")))
(beginning-of-line) (forward-line 1)))


--
__Pascal Bourguignon__ http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.

Harald Hanche-Olsen

unread,
Jul 17, 2007, 9:46:54 AM7/17/07
to
+ Pascal Bourguignon <p...@informatimago.com>:

| Rainer Stengele <rainer....@diplan.de> writes:
|> I just couldn't find a fast solution to sort a line of words:
|>
|> zzz aaa hhhh
|>
|> -->
|>
|> aaa hhhh zzz
|>
|>
|> Did I miss a simple command?
|
| AFAIK, no.
|
| But it's rather simple a command to write:

You could also pipe the line through the command

tr ' ' \n | sort | tr \n ' '

or variations thereof, depending on your shell's ability to interpret
escape sequences like \n.

--
* Harald Hanche-Olsen <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
when there is no ground whatsoever for supposing it is true.
-- Bertrand Russell

Rainer Stengele

unread,
Jul 17, 2007, 1:54:43 PM7/17/07
to
Harald Hanche-Olsen schrieb:

> + Pascal Bourguignon <p...@informatimago.com>:
>
> | Rainer Stengele <rainer....@diplan.de> writes:
> |> I just couldn't find a fast solution to sort a line of words:
> |>
> |> zzz aaa hhhh
> |>
> |> -->
> |>
> |> aaa hhhh zzz
> |>
> |>
> |> Did I miss a simple command?
> |
> | AFAIK, no.
> |
> | But it's rather simple a command to write:
>
> You could also pipe the line through the command
>
> tr ' ' \n | sort | tr \n ' '
>
> or variations thereof, depending on your shell's ability to interpret
> escape sequences like \n.
>

thanx!

calling external processes to sort a line seems to me quite a little
overhead.

rainer

Rainer Stengele

unread,
Jul 17, 2007, 1:57:17 PM7/17/07
to
Pascal Bourguignon schrieb:

> Rainer Stengele <rainer....@diplan.de> writes:
>> I just couldn't find a fast solution to sort a line of words:
>>
>> zzz aaa hhhh
>>
>> -->
>>
>> aaa hhhh zzz
>>
>>
>> Did I miss a simple command?
>
> AFAIK, no.
>
> But it's rather simple a command to write:
>
> (defun sort-words-in-lines (start end)
> (interactive "r")
> (goto-char start)
> (beginning-of-line)
> (while (< (setq start (point)) end)
> (let ((words (sort (split-string (buffer-substring start (line-end-position)))
> (function string-lessp))))
> (delete-region start (line-end-position))
> (dolist (word words ) (insert word " ")))
> (beginning-of-line) (forward-line 1)))
>
>
thanx!

Wow! Thats quite a piece of code for a not-yet Lisp programmer.
Coming from perl this would be a simple one-liner doing the work.
Don't misunderstand me - I understand the power and flexibility of elisp
in emacs. I just wonder if there is not a built in solution.

Somebody?

rainer

Pascal Bourguignon

unread,
Jul 17, 2007, 2:07:52 PM7/17/07
to
Rainer Stengele <rainer....@online.de> writes:

Of course there is a one-liner!

You select your lines, and type M-x sort-words-in-lines RET

Rainer Stengele

unread,
Jul 17, 2007, 2:20:52 PM7/17/07
to
yes - ok.

btw - your code adds whitespaces. This works:

(defun sort-words-in-lines (start end)
(interactive "r")
(goto-char start)
(beginning-of-line)
(while (< (setq start (point)) end)
(let ((words (sort (split-string (buffer-substring start
(line-end-position)))
(function string-lessp))))
(delete-region start (line-end-position))
(dolist (word words ) (insert word " "))

(delete-trailing-whitespace))
(beginning-of-line) (forward-line 1)))

Thien-Thi Nguyen

unread,
Jul 17, 2007, 3:20:03 PM7/17/07
to
() Rainer Stengele <rainer....@online.de>
() Tue, 17 Jul 2007 19:57:17 +0200

simple one-liner doing the work.

perhaps one expression, nicely (readably) laid out, would please you:

(defun sort-words-on-line ()
(interactive)
(insert (mapconcat 'identity
(sort (split-string
(delete-and-extract-region
(point) (1+ (line-end-position))))
'string<)
" ")
"\n"))

for this command to work, you have to be at bol and the line must end
w/ newline. in exchange for these restrictions, it has the feature of
moving you to the next line automatically (handy for kbd macro use).

thi

Harald Hanche-Olsen

unread,
Jul 17, 2007, 4:12:07 PM7/17/07
to
+ Rainer Stengele <rainer....@online.de>:

| Harald Hanche-Olsen schrieb:


|> You could also pipe the line through the command
|>
|> tr ' ' \n | sort | tr \n ' '
|

| calling external processes to sort a line seems to me quite a little
| overhead.

It all depends. For example, it depends on how often you need to do
this thing. If you do it only very rarely, I would assume that the
savings of your time (seconds? minutes?) is more valuable than the
wasted milliseconds of CPU time. But, your milage may vary, as they
say. Perhaps your time is worthless, and CPU cycles are really
valuable. Lots of people behave like this is so. I fall into the
trap myself from time to time.

Nikolaj Schumacher

unread,
Jul 17, 2007, 6:43:55 PM7/17/07
to help-gn...@gnu.org
Rainer Stengele <rainer....@online.de> wrote:

> Wow! Thats quite a piece of code for a not-yet Lisp programmer.
> Coming from perl this would be a simple one-liner doing the work.

Note that the actual work is in fact one line:
(sort (split-string ...) (function string-lessp))

The rest is just input and output.


regards,
Nikolaj Schumacher


Mathias Dahl

unread,
Jul 18, 2007, 3:21:37 AM7/18/07
to
Rainer Stengele <rainer....@online.de> writes:

> Wow! Thats quite a piece of code for a not-yet Lisp programmer.
> Coming from perl this would be a simple one-liner doing the work.

Well, what is a one-liner? I mean, behind each one-liner in perl lies
probably a lot of code that implements that feature in perl. The
difference here is that the particular feature you wanted wasn't
implemented in Emacs. Yet.

> Don't misunderstand me - I understand the power and flexibility of
> elisp in emacs. I just wonder if there is not a built in solution.

Some day maybe someone will propose this command to be added to Emacs
and then it will be "built-in"... :)

And, if you paste Pascal's example into your .emacs file it will, for
all practical purposes, built-in.

Rainer Stengele

unread,
Jul 18, 2007, 3:25:14 PM7/18/07
to
Mathias Dahl schrieb:

agreed. for any more than trivial function this is the way to go.
for "simple" functions I'd like to have them available "in the wild",
finding an emacs and not having my .emacs with me ...

comes down to decide whats a simlpe enough function to have it built in.

BTW Thank everybody who is maintaining a wonderful EMACS!

0 new messages